dll.config not copied to temporary asp.net files folder

前端 未结 1 1416
青春惊慌失措
青春惊慌失措 2020-12-31 11:38

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 t

相关标签:
1条回答
  • 2020-12-31 12:14

    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;
    

    The key there is:

    new Uri(Assembly.GetExecutingAssembly().GetName().CodeBase).LocalPath
    

    I came up with that solution after reading Zhaph - Ben Duguid's answer here: https://stackoverflow.com/a/2434339/103778.

    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.

    0 讨论(0)
提交回复
热议问题