Need ServiceConfiguration.cscfg to populate web.config sessionstate and connection strings

你说的曾经没有我的故事 提交于 2019-12-08 03:31:25

问题


I need to propagate connection string changes for entity framework, asp.net membership (which are both in the connectionstrings section of web.config) and session state (which is in sessonstate's sqlconnectionstring) in web.config when I adjust these settings in windows azure's service configuration.

During development we test our app as a standard asp.net webforms app, but once it is deployed it is running in azure. So we need to allow for the site running in both non-azure and an azure context. That's why we're just relying upon the values in web.config for now.Since these connection strings are not called directly in my code writing a utility class which grabs from azure service config if that is available or otherwise grabs from web.config is not a possibility for these values.

I realize that editing web.config would cause a disruption in service - and i only plan to do this during off hours.


回答1:


I believe that the best approach is to wrap your configuration information in a service. Then, in the service, use RoleEnvironment to determine which settings to use. For example

public static class Config
{
    public static string ConnStr
    {
        get
        {
            if (RoleEnvironment.IsAvailable)
                return RoleEnvironment.GetConfigurationSettingValue("ConnStr");

            return ConfigurationManager.AppSettings["ConnStr"];
        }
    }
}

If that doesn't work, and you need to change the actual web.config (for instance, using named connection strings), then you'll need to modify the config at runtime. In your role start, do something like the following:

var config = WebConfigurationManager.OpenWebConfiguration(null);
var connStrs = WebConfigurationManager.OpenWebConfiguration(null).GetSection("connectionStrings") as ConnectionStringsSection;
connStrs.ConnectionStrings["ConnStr"].ConnectionString = RoleEnvironment.GetConfigurationSettingValue("ConnStr");
config.Save();

To handle when the configuration changes after the role is running, just call the same code as above from the RoleEnvironment.Changing event.

Good luck,

Erick



来源:https://stackoverflow.com/questions/16042212/need-serviceconfiguration-cscfg-to-populate-web-config-sessionstate-and-connecti

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!