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.