相关疑难解决方法(0)

在运行时更改默认app.config

我有以下问题:
我们有一个加载模块的应用程序(添加).这些模块可能需要app.config中的条目(例如WCF配置).因为模块是动态加载的,所以我不希望在我的应用程序的app.config文件中包含这些条目.
我想做的是以下内容:

  • 在内存中创建一个新的app.config,它包含模块中的配置部分
  • 告诉我的应用程序使用新的app.config

注意:我不想覆盖默认的app.config!

它应该透明地工作,以便例如ConfigurationManager.AppSettings使用该新文件.

在我这个问题的评价,我用同样的解决方案提出了为这里提供:刷新的app.config与NUnit的.
不幸的是,它似乎没有做任何事情,因为我仍然从正常的app.config获取数据.

我用这段代码来测试它:

Console.WriteLine(ConfigurationManager.AppSettings["SettingA"]);
Console.WriteLine(Settings.Default.Setting);

var combinedConfig = string.Format(CONFIG2, CONFIG);
var tempFileName = Path.GetTempFileName();
using (var writer = new StreamWriter(tempFileName))
{
    writer.Write(combinedConfig);
}

using(AppConfig.Change(tempFileName))
{
    Console.WriteLine(ConfigurationManager.AppSettings["SettingA"]);
    Console.WriteLine(Settings.Default.Setting);
}
Run Code Online (Sandbox Code Playgroud)

它打印相同的值twices,虽然combinedConfig包含除正常app.config之外的其他值.

.net c# app-config

128
推荐指数
4
解决办法
8万
查看次数

将app.config加载到AppDomain中

我无法将App.Config文件加载到App域中.

我正在使用

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $config_path)
Run Code Online (Sandbox Code Playgroud)

来自Powershell使用App.config调用.NET程序集,但仍未加载App.Config文件.

我也尝试重置缓存,如使用CurrentDomain.SetData("APP_CONFIG_FILE")中所述,在PowerShell ISE中不起作用.

这是我的测试脚本:

$configFile = "{ActualPhysicalPath}\App.Config"
gc $configFile

Add-Type -AssemblyName System.Configuration
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)

[Configuration.ConfigurationManager]::ConnectionStrings[0].Name
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $null)
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configFile)
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name
Run Code Online (Sandbox Code Playgroud)

我总是得到存储在machine.config中的连接字符串,而不是App.config中的连接字符串.

如何在应用程序域中加载我的特定App.Config文件?

powershell appdomain

14
推荐指数
1
解决办法
6317
查看次数

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

我有一个包含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");
Run Code Online (Sandbox Code Playgroud)

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

ConfigurationManager.ConnectionStrings[connectionStringName]
Run Code Online (Sandbox Code Playgroud)

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

我创建了一个调用程序集的ps1 并命中该属性.那个原型可以很好地找到连接字符串.

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

subsonic powershell

3
推荐指数
1
解决办法
4478
查看次数

标签 统计

powershell ×2

.net ×1

app-config ×1

appdomain ×1

c# ×1

subsonic ×1