[英]Custom MSBuild Task not reading from App.config [C#]
I wrote a custom build task that reads values form the appsettings in it's App.config file. 我编写了一个自定义构建任务,从其App.config文件中的appsettings读取值。 When I compile my task as an executable and run it, the task works perfectly.
当我将任务编译为可执行文件并运行它时,任务完美无缺。 The correct values are read from the config file.
从配置文件中读取正确的值。 However when I compile it as an assembly and run it from a target in my build script I get a System.NullReferenceException.
但是当我将它编译为程序集并从我的构建脚本中的目标运行它时,我得到一个System.NullReferenceException。 The exception occurs in the foreach loop because the configuration manager returns null.
foreach循环中发生异常,因为配置管理器返回null。
IEnumerable<string> tables = ConfigurationManager.AppSettings.GetValues(key);
foreach (string txt in tables)
{
Logic.....
}
I'm calling the custom task correctly because I commented out the issue and it builds successfully. 我正在调用自定义任务,因为我已经注释掉了问题并且它已成功构建。
Does anyone know why this might be happening? 有谁知道为什么会发生这种情况? or if I'm even able to use a App.config file with custom build tasks?
或者如果我甚至能够使用自定义构建任务的App.config文件?
Thanks in advance 提前致谢
If you compile a project as a library, then it reads from the app.config of the calling executable. 如果将项目编译为库,则它将从调用可执行文件的app.config中读取。 If you compile a project as an executable, then it reads from it's own app.config.
如果您将项目编译为可执行文件,那么它将从它自己的app.config中读取。
If anyone is interested I used the following code to access the custom config 如果有人感兴趣我使用以下代码来访问自定义配置
private static string[] GetConfigFile()
{
var map = new ExeConfigurationFileMap();
map.ExeConfigFilename = @"C:\ConfigFile.config";
config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
return config.AppSettings.Settings.AllKeys;
}
The above code gets the list of keys from the specified config file. 上面的代码从指定的配置文件中获取密钥列表。 The return value is stored in a string array which I run through using a foreach loop as seen below
返回值存储在一个字符串数组中,我使用foreach循环运行,如下所示
string[] keyNames = GetConfigFile();
foreach (string keys in keyNames )
{
KeyValueConfigurationElement keyval = config.AppSettings.Settings[keys];
Console.WriteLine(keyval.Value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.