【问题标题】:Configure ASP.NET SQL Membership Provider from Web Role setting从 Web 角色设置配置 ASP.NET SQL 成员资格提供程序
【发布时间】:2012-06-21 20:21:53
【问题描述】:

我有一个使用 ASP.NET SQL 成员资格提供程序的 Web 角色。当前配置位于 Web.Config 文件中。我想将连接字符串配置为 Web 角色设置,而不是将其放在 Web.Config 中。我想要这样做的主要原因是我可以在 azure 项目上设置配置以发布到不同的托管服务(dev、qa 等)。现在,每次我想手动编辑 web.config发布到不同的服务。

我对如何实现这一点有一些想法。

  1. 编写自定义成员资格提供程序,包装 SQL 提供程序并为其提供自定义配置。
  2. 在 Web Role OnStart 方法中添加一些内容以更改 Web.config 文件中的连接字符串。

以前有没有人做过类似的事情,或者对哪个选项可能是最好的有建议,或者对如何实现这一点有其他想法?

【问题讨论】:

    标签: asp.net azure asp.net-membership sqlmembershipprovider


    【解决方案1】:

    Windows Azure SDK 允许您在每个环境中拥有多个服务配置。但是 web.config 修改有点困难。在您的情况下,我建议您编写一些代码(或启动任务),从服务配置中读取连接字符串并将其写入 web.config。

    Andy 的博文“Programmatically modify web.config on WebRole Startup”详细解释了如何做到这一点:

    public override bool OnStart()
    {
        using (var server = new ServerManager())
        {
            // get the site's web configuration
            var siteNameFromServiceModel = "Web"; // TODO: update this site name for your site. 
            var siteName =
                string.Format("{0}_{1}", RoleEnvironment.CurrentRoleInstance.Id, siteNameFromServiceModel);
            var siteConfig = server.Sites[siteName].GetWebConfiguration();
    
            // get the appSettings section
            var appSettings = siteConfig.GetSection("appSettings").GetCollection();
    
            AddElement(appSettings, "deploymentId", RoleEnvironment.DeploymentId);
            AddElement(appSettings, "internalEndpointPort", RoleEnvironment.CurrentRoleInstance.InstanceEndpoints
                .First(t=>t.Key=="InternalEndpoint1").Value
                .IPEndpoint.Port.ToString());
    
            server.CommitChanges();
        }
        return base.OnStart();
    }
    

    【讨论】:

    • 在部署到 Azure 时是否忽略常规 Web.Config 文件,并从 GUI 获取设置(我是 Azure 新手)?
    • 博文中没有提到的一件事是,您必须以提升的权限运行您的角色才能保存配置更改。
    • IrishChieftain,Web.config 始终被部署(它仍然是您的 ASP.NET Web 应用程序正常工作所必需的),但是使用此代码,您可以使用 ServiceConfiguration 中的设置对其进行修改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 1970-01-01
    相关资源
    最近更新 更多