C
C#2y ago
Raki

❔ How to read configuration based on environment

I have a stored my connection string as configuration in web.config. Now I have stored the same connection string as application setting variable in app service. So now how to make my application to read from web.config while in dev environment and from app service which its deployed. Existing code to get the connectionstring:

public string GetConnectionString(string service)
{
var _tenantSettingsKey = _shellSettings.Name + ":" + service;
var _defaultSettingsKey = service;
var connectionStringSettings = ConfigurationManager.ConnectionStrings[_tenantSettingsKey] ?? ConfigurationManager.ConnectionStrings[_defaultSettingsKey];
if (connectionStringSettings == null)
{
throw new ConfigurationErrorsException("A connection string is expected for " + service);
}
return connectionStringSettings.ConnectionString;
}

public string GetConnectionString(string service)
{
var _tenantSettingsKey = _shellSettings.Name + ":" + service;
var _defaultSettingsKey = service;
var connectionStringSettings = ConfigurationManager.ConnectionStrings[_tenantSettingsKey] ?? ConfigurationManager.ConnectionStrings[_defaultSettingsKey];
if (connectionStringSettings == null)
{
throw new ConfigurationErrorsException("A connection string is expected for " + service);
}
return connectionStringSettings.ConnectionString;
}
1 Reply
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server