我有一个 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 文件的属性设置为“构建操作:内容”、“复制到输出目录:始终”
任何帮助将不胜感激,现在已经用谷歌搜索了几个小时。 :(
最好的祝福,
彼得拉尔森!
最佳答案
我通过使用以下代码解决了这个问题:
// 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 配置文件。我的网络应用程序的目录。我从 written up a blog post进一步讨论这个。
关于asp.net - dll.config 未复制到临时 asp.net 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17167307/