【问题标题】:dll.config not copied to temporary asp.net files folderdll.config 未复制到临时 asp.net 文件夹
【发布时间】:2013-06-14 13:47:54
【问题描述】:

我有一个使用来自不同程序集的函数的 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 文件的属性设置为“构建操作:内容”、“复制到输出目录:始终”

任何帮助将不胜感激,现在已经搜索了几个小时。 :(

最好的问候, 彼得拉尔森!

【问题讨论】:

    标签: asp.net configurationmanager


    【解决方案1】:

    我使用以下代码解决了这个问题:

    // 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 进一步讨论了这个问题。

    【讨论】:

    • 这对我帮助很大。我还注意到对 GetName() 的调用是无用的,因为属性 CodeBase 也是类 Assembly 的属性。
    • 救命稻草!我在寻找解决这个问题的方法。遵循了很多没有用的建议。也许对于谷歌的结果:我有一个使用 Entity Framework 的带有数据库连接的 DLL,并且连接字符串在 dll.config 中。
    猜你喜欢
    • 2017-08-07
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多