Pet*_*son 11 asp.net configurationmanager
我有一个Web.Api应用程序,它使用来自不同程序集的函数.对于这个程序集,我创建了一个.config文件,我存储了一些字符串.
我使用以下代码来获取其中一个字符串:
private static string LogUrl = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings.Settings["WebApi-LogUrl"].Value.ToString();
Run Code Online (Sandbox Code Playgroud)
Assembly.GetExecutingAssembly().Location指向临时的asp.net文件,(C:\ Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\dc2fa3d4\834ee436\assembly\dl3\cd068512)但我的dll.config文件不是在那里复制.结果是我无法调试我的应用程序,并且在真正的IIS服务器上运行代码时它也给出了null.
如果我在获取设置之前设置了一个断点,我可以抓住临时文件夹,当我复制我的dll.config文件时,一切正常,但我应该如何自动完成.
我将我的dll.config文件的属性设置为"构建操作:内容","复制到输出目录:始终"
任何帮助将不胜感激,现在谷歌搜索了几个小时.:(
最诚挚的问候,Peter Larsson!
dwh*_*ite 22
我通过使用以下代码解决了这个问题:
// The dllPath can't just use Assembly.GetExecutingAssembly().Location as ASP.NET doesn't copy the config to shadow copy path
var dllPath = new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath;
var dllConfig = ConfigurationManager.OpenExeConfiguration(dllPath);
// Get the appSettings section
var appSettings = (AppSettingsSection) dllConfig.GetSection("appSettings");
return appSettings.Settings;
Run Code Online (Sandbox Code Playgroud)
关键是:
new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath
Run Code Online (Sandbox Code Playgroud)
在阅读Zhaph-Ben Duguid的回答后,我想出了解决方案:https://stackoverflow.com/a/2434339/103778.
现在我可以拿起bin我的网络应用程序目录中的DLL配置文件.
我已经写了一篇博文,进一步讨论这个问题.
| 归档时间: |
|
| 查看次数: |
2814 次 |
| 最近记录: |