【问题标题】:Need ServiceConfiguration.cscfg to populate web.config sessionstate and connection strings需要 ServiceConfiguration.cscfg 来填充 web.config 会话状态和连接字符串
【发布时间】:2013-04-09 03:54:49
【问题描述】:

当我调整这些设置时,我需要在 web.config 中传播实体框架、asp.net 成员资格(都在 web.config 的 connectionstrings 部分中)和会话状态(在 sessonstate 的 sqlconnectionstring 中)的连接字符串更改在 windows azure 的服务配置中。

在开发过程中,我们将我们的应用程序作为标准的 asp.net webforms 应用程序进行测试,但一旦部署,它就会在 azure 中运行。因此,我们需要允许站点在非天蓝色和天蓝色上下文中运行。这就是为什么我们现在只依赖 web.config 中的值。因为在我的代码中没有直接调用这些连接字符串,因此编写了一个实用程序类,该类从 azure 服务配置中获取(如果可用,或者从 web.config 中获取)是这些值是不可能的。

我意识到编辑 web.config 会导致服务中断 - 我只打算在非工作时间这样做。

【问题讨论】:

    标签: entity-framework azure asp.net-membership connection-string azure-configuration


    【解决方案1】:

    我认为最好的方法是将配置信息包装在服务中。然后,在服务中,使用 RoleEnvironment 来确定要使用的设置。例如

    public static class Config
    {
        public static string ConnStr
        {
            get
            {
                if (RoleEnvironment.IsAvailable)
                    return RoleEnvironment.GetConfigurationSettingValue("ConnStr");
    
                return ConfigurationManager.AppSettings["ConnStr"];
            }
        }
    }
    

    如果这不起作用,并且您需要更改实际的 web.config(例如,使用命名连接字符串),那么您需要在运行时修改配置。在您的角色开始时,请执行以下操作:

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

    要处理角色运行后配置发生变化时,只需从 RoleEnvironment.Changing 事件中调用与上述相同的代码即可。

    祝你好运,

    埃里克

    【讨论】:

    • 感谢示例代码。仅在部署实例时调整角色启动 - 但我的实例可能已经部署并运行 - 我需要在实例运行时更改 web.config - 你有一些例子吗?
    • 每当角色配置发生变化时,您都需要调用更新 web.config 的代码。 RoleEnvironment.Changing 事件是您想要使用的。
    猜你喜欢
    • 2011-09-19
    • 2010-11-22
    • 2012-05-12
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 2016-08-11
    相关资源
    最近更新 更多