从Powershell脚本中引用的DLL对App.Config连接字符串进行亚音速访问

时间:2010-05-07 16:00:11

标签: powershell subsonic

我有一个包含Subsonic生成和增强代码的DLL来访问数据模型。实际上,它是原始程序集的合并DLL,Subsonic本身和一些其他引用的DLL到一个程序集中,称为“PowershellDataAccess.dll。但是,应该注意的是,我也尝试过这个引用每个程序集脚本也是如此,也不起作用。

然后我尝试使用该程序集中的对象和方法。在这种情况下,我正在访问一个使用Subsonic来加载一堆记录并从这些记录中创建Lucene索引的类。

我遇到的问题是调用Subsonic方法从数据库中检索数据说它无法找到连接字符串。我将AppDomain指向适当的配置文件,该文件确实包含该连接字符串。

这是剧本。

$ScriptDir = Get-Location
[System.IO.Directory]::SetCurrentDirectory($ScriptDir)
[Reflection.Assembly]::LoadFrom("PowershellDataAccess.dll")
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$ScriptDir\App.config")
$indexer = New-Object LuceneIndexingEngine.LuceneIndexGenerator
$indexer.GeneratePageTemplateIndex("PageTemplateIndex");

我开始深入研究Subsonic本身,而Subsonic中的以下行正在寻找连接字符串并抛出异常:

ConfigurationManager.ConnectionStrings[connectionStringName]

因此,出于好奇,我创建了一个具有单个类的程序集,该类具有一个只运行该行的属性以检索连接字符串名称。

我创建了一个名为 程序集的ps1并点击该属性。那个原型可以很好地找到连接字符串。

任何人都知道为什么Subsonic的部分似乎看不到连接字符串?

1 个答案:

答案 0 :(得分:7)

您是否将System.Configuration程序集添加到PowerShell会话中?以下适用于我:

PS> gc .\app.config

<?xml version='1.0' encoding='utf-8'?>
<configuration>
    <connectionStrings>
      <clear />
      <add name="Name"
           providerName="System.Data.ProviderName"
           connectionString="Valid Connection String;" />
    </connectionStrings>
</configuration>

PS> [appdomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$home\app.config")
PS> Add-Type -AssemblyName System.Configuration
PS> [Configuration.ConfigurationManager]::ConnectionStrings['Name']

Name                    : Name
ConnectionString        : Valid Connection String;
...
相关问题