我正在开发一个数据访问组件,该组件将用于包含经典ASP和ASP.NET页面混合的网站,并且需要一种管理其配置设置的好方法.
我想使用自定义ConfigurationSection
,对于ASP.NET页面,这非常有用.但是当从传统的ASP页面通过COM interop调用组件时,组件不会在ASP.NET请求的上下文中运行,因此不了解web.config.
有没有办法告诉ConfigurationManager
只需从任意路径加载配置(例如,..\web.config
如果我的程序集在/bin
文件夹中)?如果有,那么我认为我的组件可以回退到如果默认ConfigurationManager.GetSection
返回null
我的自定义部分.
任何其他方法都欢迎!
是否可以将整个App.Config文件重定位到自定义路径?
配置文件与exe文件位于同一文件夹中似乎有点奇怪,Windows的新approcah将所有程序设置保存在c:\ ProgramData和all中.
我们的另一个要求是以编程方式指定在哪里找到app.config文件.这样做的原因是我们从同一个exes生成不同的服务实例,并希望将每个服务的app.config存储在c:\ ProgramData \\下该服务的settings文件夹中.
有没有人研究过如何让PowerShell使用app.config
文件?我有一些.NET DLL我想在我的一个脚本中使用,但是他们希望自己的配置部分存在于app.config
/中web.config
.
当我正在抛弃我的小宠物项目时,我正在尝试将所有常量字符串存储在我的app.config文件中(Keys,XpathExpressions等).当我运行编译的exe时,这很有效.在Interactive Shell中并非如此.
我试图将.config文件从我的bin/Release目录复制到obj/Debug&obj/Release目录,但Call to ConfigurationManager.AppSettings.Item("key")
always总是返回null.
有任何建议如何解决这个问题?
最诚挚的问候
我有以下测试:
[TestClass]
public class GeneralTest
{
[TestMethod]
public void VerifyAppDomainHasConfigurationSettings()
{
string value = ConfigurationManager.AppSettings["TestValue"];
Assert.IsFalse(String.IsNullOrEmpty(value), "No App.Config found.");
}
[TestMethod]
[HostType("Moles")]
public void VerifyAppDomainHasConfigurationSettingsMoles()
{
string value = ConfigurationManager.AppSettings["TestValue"];
Assert.IsFalse(String.IsNullOrEmpty(value), "No App.Config found.");
}
}
Run Code Online (Sandbox Code Playgroud)
它们之间的唯一区别是[HostType("Moles")]
.但第一次传球,第二次传球失败.如何从第二次测试中读取App.config?
或者我可以在其他地方添加另一个配置文件?
我有多个NUnit测试,我希望每个测试都使用特定的app.config文件.有没有办法在每次测试之前将配置重置为新的配置文件?
我试图在PowerShell ISE中使用.NET 4.0程序集,并尝试更改通过以下方式使用的配置文件:
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $PathToConfig);
Run Code Online (Sandbox Code Playgroud)
[Configuration.ConfigurationManager] :: ConnectionStrings.Count总是返回"1",
"[Configuration.ConfigurationManager] :: ConnectionStrings [0] .Name"总是返回"LocalSqlServer",而ConnectionString名称不在我的".config"中文件.
请注意,从PowerShell命令提示符执行PowerShell脚本将按预期运行.就在我从PowerShell ISE中执行它时,它无法按预期工作.
假设我正在创建一个名为ConsoleApp2的应用程序。
由于我使用了一些第三方库,我的默认 app.config 文件正在生成类似的代码
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Run Code Online (Sandbox Code Playgroud)
那是因为我的解决方案引用了一个库的不同版本,所以它需要告诉大家:“嘿,如果你要找这个库的任何旧版本,就用新版本”。没关系。
问题是我想定义一个单独的配置文件“test.exe.config”,在那里我有一些设置并摆脱自动生成的设置。
为了告诉我的应用程序有关新配置文件的信息,我使用了如下代码
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "test.exe.config");
Run Code Online (Sandbox Code Playgroud)
这(几乎)完美地工作。我在那里写了“几乎”,因为虽然该<appSettings>
部分被正确读取,但该<runtime>
部分并未在我的自定义配置文件中查看,但应用程序在默认配置文件中查找它,这是一个问题,因为我想以后可以删除那个。
那么,我如何告诉我的应用程序也<runtime>
从我的自定义配置文件中读取信息?
重现我的问题的简单示例如下:
创建一个名为ClassLibrary2 ( .Net Framework v4.6 )的库,包含一个类,如下所示
using Newtonsoft.Json.Linq;
using System;
namespace ClassLibrary2
{
public class Class1
{
public Class1()
{
var json = new JObject();
json.Add("Succeed?", true);
Reash = json.ToString();
}
public String …
Run Code Online (Sandbox Code Playgroud) 我在它自己的程序集中有一个手写的WCF代理,它非常简单:
public class MyServiceClient : ClientBase<IMyService>, IMyService
{
public MyServiceClient()
{
}
public MyServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
}
Run Code Online (Sandbox Code Playgroud)
我将其加载到Powershell脚本中:
Add-Type -Path "$LocalPath\MyService.Client.dll"
Add-Type -Path "$LocalPath\MyService.Contracts.dll"
Run Code Online (Sandbox Code Playgroud)
然后我尝试设置App.config(根据SO上的其他帖子),以便客户端可以使用在config中定义的Endpoint进行实例化,而不是在脚本本身中:
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", "$LocalPath\MyService.Client.dll.config")
Run Code Online (Sandbox Code Playgroud)
我检查了AppDomain,并将配置文件设置为其ConfigurationFile
属性.
当我创建客户端的实例时:
$endpointName = "MyServiceHttpEndpoint" # defined in the app.config file
$myclient = New-Object MyService.Client.MyServiceClient($endpointName)
Run Code Online (Sandbox Code Playgroud)
它贬低说:
Exception calling ".ctor" with "1" argument(s): "Could not find endpoint element with name 'MyServiceHttpEndpoint' and contract 'MyService.Contracts.IMyService' in the ServiceModel client configuration section. This might be because no configuration file …
是否可以使用F#脚本(fsx)文件中的app.config文件?如果是这样,怎么样?我在哪里放置文件以及它将被称为什么?
.net ×5
c# ×5
app-config ×3
powershell ×2
.net-4.0 ×1
asp-classic ×1
asp.net ×1
f# ×1
f#-scripting ×1
moles ×1
mstest ×1
nunit ×1
path ×1
unit-testing ×1
wcf ×1
windows-8 ×1