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

試著忘記壹切 提交于 2019-12-08 17:36:30

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

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