![](/img/trans.png)
[英]Preventing ASP.Net projects from copying {assembly}.dll.config to bin folder
[英]dll.config not copied to temporary asp.net files folder
我有一个Web.Api应用程序,它使用来自不同程序集的函数。 对于这个程序集,我创建了一个.config文件,我存储了一些字符串。
我使用以下代码来获取其中一个字符串:
private static string LogUrl = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location).AppSettings.Settings["WebApi-LogUrl"].Value.ToString();
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!
我通过使用以下代码解决了这个问题:
// 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;
关键是:
new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath
在阅读Zhaph-Ben Duguid的回答后,我想出了解决方案: https : //stackoverflow.com/a/2434339/103778 。
现在我可以拿起我的网络应用程序的bin
目录中的DLL配置文件。
我已经写了一篇博文,进一步讨论这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.