1

I have a number of projects that need to access settings, database connection, web api url, authentication server url etc, that will change depending on its deployment.

The application could be deployed multiple times to different departments, each with distinct databases and webservers.

Initially I was using appSettings and exposing them as properties. This appeared to work in development through the ide (Visual Studio 2013).

I followed suggestions here in order to get the location correctly and it seemed to work. So Initially I had:

  private static KeyValueConfigurationCollection GetAppSettings()
    {
        // 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;
    }

 public string AknowledgeSTSOrigin
        {
            get
            {
                string setting;
                if (_aknowledgeSTSOrigin != null)
                {
                    setting = _aknowledgeSTSOrigin;
                }
                else
                {
                    if (System.Diagnostics.Debugger.IsAttached)
                    {
                        setting = "https://localhost:44333";
                    }
                    else
                    {
                        var settings = GetAppSettings();
                        if (settings.Count > 0)
                        {
                            setting = settings["AknowledgeSTSOrigin"].Value;
                        }
                        else
                        {
                            setting = System.Configuration.ConfigurationManager.AppSettings["AknowledgeSTSOrigin"];
                        }
                    }
                     _aknowledgeSTSOrigin =setting;
                }
                return setting;
            }
        }

AT some point it stopped working - but unfortunately I've no idea when or why because I was running in the ide so it was always defaulting to the debugger attached condition. The problem at this point was that all the above routes were returning null from appSettings. When searching for the problem, I found that it's now recommended to use the project properties. I did so - a new ApplicationSettings section appeared in app.config and the properties were found. So the above got simplified to:

setting = Properties.Settings.Default.AknowledgeSTSOrigin;

This worked great, I set the values to localhost to test running in ide - all good. I compiled and Published the app, then tried to change app.config on the server. It still wouldn't work on the server. But if I changed app.config on my machine to have the server settings, rebuilt and published it, it then works.

So it appears that this method gets the properties from app.config at compile time.

What I want to do is to be able to get them from config file at runtime. So build the app once, deploy multiple times and then just update the config with deployment specific settings.

I've tried googling this and checking SO, but not finding anything useful.

tried adding

 Properties.Settings.Default.Reload();

as per this post but not working.

Target Framework = .Net 4.5 and its a class library. The error message shows that its still getting the value from the config file at compile time. (Error message not relevant as if it had the correct path there wouldn't be an error)

EDIt: getting even more confused now. Found the following post which mentioned that the default is for the properties to be compiled unless you set GenerateDefaultValueInCode to false. I've done this and I'm now back to the same result as it returning null for the properties. And for some reason, despite setting the value to false for all the properties they are still visible embedded within the dll.

SteveL
  • 857
  • 10
  • 18
  • app.config settings are read into memory when an application starts. You can 100% change app config settings without recompiling I have done it many times. In IIS hosted apps the app pool should be recycled automatically, but responses may be cached. To be safe restart the application pool manually. Windows services and other applications probably need to be restarted too – ste-fu Jun 28 '17 at 14:03
  • @ste-fu I've recycled the app pool, restarted the application and even rebooted the server. I have two servers - identical save for name. Deploy to the one I compiled against - works fine. Deploy to the other - doesn't pick up settings from config. – SteveL Jun 28 '17 at 15:11
  • What's the name of the .config file you're editing? Is it YourAppliacationName.exe.config *not* app.config? – aquinas Jun 28 '17 at 15:27

1 Answers1

0

Found It! The problem was occurring in a class library. Although I create the settings in the class library and the settings get added to the app.config, it doesn't actually look there to retrieve them. Instead it's the web.config of the parent consuming the class library. I put the settings in web.config and it appears to work.

SteveL
  • 857
  • 10
  • 18