从 Powershell 脚本中引用的 DLL 对 App.Config 连接字符串的亚音速访问 [英] Subsonic Access To App.Config Connection Strings From Referenced DLL in Powershell Script

查看:33
本文介绍了从 Powershell 脚本中引用的 DLL 对 App.Config 连接字符串的亚音速访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've got a DLL that contains Subsonic-generated and augmented code to access a data model. Actually, it is a merged DLL of that original assembly, Subsonic itself and a few other referenced DLL's into a single assembly, called "PowershellDataAccess.dll. However, it should be noted that I've also tried this referencing each assembly individually in the script as well and that doesn't work either.

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

I am then attempting to use the objects and methods in that assembly. In this case, I'm accessing a class that uses Subsonic to load a bunch of records and creates a Lucene index from those records.

我遇到的问题是调用 Subsonic 方法以从数据库检索数据时说它找不到连接字符串.我将 AppDomain 指向相应的配置文件,该文件包含该连接字符串(按名称).

The problem I'm running into is that the call into the Subsonic method to retrieve data from the database says it can't find the connection string. I'm pointing the AppDomain at the appropriate config file which does contain that connection string, by name.

这是脚本.

$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 中的以下行是查找连接字符串并抛出异常的内容:

I went digging into Subsonic itself and the following line in Subsonic is what's looking for the connection string and throwing the exception:

ConfigurationManager.ConnectionStrings[connectionStringName]

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

So, out of curiosity, I created an assembly with a single class that has a single property that just runs that one line to retrieve the connection string name.

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

I created a ps1 that called that assembly and hit that property. That prototype can find the connection string just fine.

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

Anyone have any idea why Subsonic's portion can't seem to see the connection strings?

推荐答案

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

Did you add the System.Configuration assembly to your PowerShell session? The following works for me:

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;
...

这篇关于从 Powershell 脚本中引用的 DLL 对 App.Config 连接字符串的亚音速访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆