问题
I am maintaining an ASP.NET MVC 4 project which references many Class Libraries that have their own app.config with settings managed through the Settings.settings UI. I have read many questions on Stack Overflow and recently this article and this SO question.
It was my understanding that the Web.config of the web application in my MVC 4 project would be what all referenced projects would look at for the settings. You just have to make sure the same settings are specified in the main web.config class.
So I created my own class library with settings. I have a static class that just prints out Footer information for all of the applications:
public static class Footer
{
public static string DatabaseEnvironment
{
get { return new DAL().GetCurrentEnvironment();}
}
public static string CurrentApplicationVersion
{
get { return Properties.Settings.Default.ApplicationVersion; }
}
public static string CurrentYear
{
get { return DateTime.Today.Year.ToString(); }
}
}
In the class library I defaulted the ApplicationVersion setting to 0. Then in the application referencing this library, I set the correct version. Well when I run the application the version shows 0. I am confused as this undermines everything I am reading. I am really confused as to what configuration file takes precedence. I don't see the app.config file in the bin folder or root folder of the built web application project. I did set a breakpoint on the referenced project to see the settings, and it only shows the three settings defined in that referenced project, not the ones from the web.config like I thought would show up.
So, does the Web.config class in an MVC project not take precedence over the class library defined settings in the app.config file?
Update opening up the Footer.dll it looks like the settings are being compiled into it?
Update2 Well there is a property called GenerateDefaultValueInCode on each setting in the Settings.settings file, if you set it to true (which is the default value) it generates the value in the code as described. This is supposed to be the fallback value for when it can't find a reference elsewhere in the main project. I am not sure what the other articles are referencing but using the Settings.settings UI which stores the setting under the in the web.config or app.config doesn't lead to the referenced class libraries relying on the web.config of the main web application. I realize I can go about other ways of doing this but that would require reworking many class libraries to adhere to the new design. I was hoping for a more transparent solution without having to change much code. I also realize this stuff might be old news to some, but from the search hits, it seemed all I had to do was create the same setting in the web.config to override the app.config settings. I guess it's not that easy.
I am trying to overcome having to change the many settings to production values when deploying to production vs Test. But this is turning out to be more work than originally thought.
来源:https://stackoverflow.com/questions/54219286/referenced-class-library-application-settings-in-app-config-are-being-used-over