dll.config没有复制到临时asp.net文件文件夹 [英] dll.config not copied to temporary asp.net files folder
问题描述
我有一个Web.Api应用程序,该应用程序使用来自不同程序集的功能.对于该程序集,我创建了一个.config文件,其中存储了一些字符串.
I have an Web.Api application that uses functions from a different assembly. For this assembly I have created a .config file where I store some strings.
我正在使用以下代码,该代码应获取这些字符串之一:
I am using the following code which should fetch one of those strings:
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.
Assembly.GetExecutingAssembly().Location
points to temporary asp.net files, (C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\dc2fa3d4\834ee436\assembly\dl3\cd068512) but my dll.config file is not copied there.
The result is that I cant debug my application and it also gives null when running the code on a real IIS server.
如果我在设置之前就设置了一个断点,则可以保留该临时文件夹,并且当我复制dll.config文件时,它都可以使用,但是我应该如何自动做到这一点.
If I set a break point just before getting the setting I can get hold of the temporary folder, and when I copy my dll.config file there it all works, but how should I make do that automatically.
我将dll.config文件的属性设置为构建操作:内容",复制到输出目录:始终"
I have the properties for my dll.config file set as "Build action: content", "Copy to output directory: always"
任何帮助将不胜感激,已经在Google上搜索了几个小时.:(
Any help would be appreciated, have googled for hours now. :(
最诚挚的问候,彼得·拉尔森!
Best regards, Peter Larsson!
推荐答案
我通过使用以下代码解决了这个问题:
I solved this by using the following code:
// 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 的答案后,我想到了该解决方案:
I came up with that solution after reading Zhaph - Ben Duguid's answer here: https://stackoverflow.com/a/2434339/103778.
现在,我可以在我的Web应用程序的 bin
目录中拾取DLL的配置文件.
Now I'm able to pick up my DLL's configuration file that is in the bin
directory of my web app.
I have since written up a blog post discussing this further.
这篇关于dll.config没有复制到临时asp.net文件文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!