4

In one of the application i am developing on ASP.Net. In this application we have been using lots of AppSettings. In the initial development we used ConfigurationManager.AppSettings[""]. but as development progressed we created a utility class in which we would define a static property for each AppSettings. Then issues started to come. Now when application is deployed on testing server and we change any settings on AppSettings it does not have any effect unless we restart the IIS. here is the following code snippet i am using to create static property of AppSettings.

public static class AppSettingsUtil
{
      public static string Log4Net
      {
          get
          {
              return ConfigurationManager.AppSettings["Log4Net"];
          }
      }
}

One of the reason i could think of is that , It is a static property so it may be initialized once in its lifetime so next time onwards it may not be fetching values from appsettings .

Chintan Shah
  • 3,133
  • 4
  • 30
  • 39

3 Answers3

11

I know this is an old thread, but something to add.

If you use:

<appSettings file="AppSettings.config" />

Then changes to the external file will not be available until a change to web.config is made or a restart is performed.

But if you change that to:

<appSettings configSource="AppSettings.config" />

The changes to those settings are available in your code immediately without a restart or a web.config change.

I just verified that this is the case with a repeatable test.

Ed DeGagne
  • 3,250
  • 1
  • 30
  • 46
  • Nice addition. This will help a lot. – Chintan Shah Jan 30 '12 at 10:44
  • 2
    One thing to be aware of. If you use configSource, then you cannot have any entries in the appSettings portion of your web.config. Only in your external file. If you use the file= approach, then you can have entries in both locations... but you have to restart IIS (or change the web.config) for your external file to be read. – Eric Burdo Jun 18 '13 at 17:34
  • 1
    Thanks for explaining the "file" vs "configSource" bit Ed! I was banging my head till I found that! – Christopher Oct 07 '16 at 12:42
  • See also [ASP.NET web.config: configSource vs. file attributes](https://stackoverflow.com/a/6940086/18192) – Brian Aug 25 '17 at 18:01
3

You clearly are deploying the config file since after restarting IIS the updated config is available.

It sounds like you are using an external file for your AppSettings:

<appSettings file="my-app-settings.config" />

This is good for keeping web.config clean, particularly if you maintain different config files for separate environment (e.g., dev, testing, prod). However, the catch to this approach is that ASP.NET does not detect changes to the external file automatically, so your settings are not automatically refreshed.

Per MSDN, on .NET Framework 2.0 changes to the separate file do not cause subsequent application restarts. It sounds like your solution is going to be to not use the external file in this scenario.

Phil Hunt
  • 8,404
  • 1
  • 30
  • 25
  • Not 100% correct, see my answer below. Do not use the "file" attribute, use the "configSource" attribute instead. – Ed DeGagne Jan 25 '12 at 17:20
0

Static methods in that context would never cache the value coming from the config without using one of the AOP frameworks. The more likely scenario is the site is not being compiled and moved into the framework directories. On a personal note, I prefer to call that file Config.cs.

Yuriy Faktorovich
  • 67,283
  • 14
  • 105
  • 142