【问题标题】:ASP.NET Universal Providers + Azure CSCFGASP.NET 通用提供程序 + Azure CSCFG
【发布时间】:2012-08-01 03:07:05
【问题描述】:
【问题讨论】:
标签:
asp.net
azure
web-config
asp.net-membership
【解决方案1】:
我认为您不能让通用提供程序从 ServiceConfiguration(与 web.config 相对)中读取。但是您可以做的是在每次部署应用程序或每次修改 ServiceConfiguration 时使用来自 ServiceConfiguration 的信息修改 web.config。
在您的 WebRole.cs 中,您将首先编写一些执行此操作的代码。有一个topic on MSDN 解释了如何做到这一点:
- 在提升模式下运行
- 参考 %WINDIR%\system32\inetsrv\Microsoft.Web.Administration.dll
-
在 OnStart 方法中写入(您需要更改此代码):
using (var server = new ServerManager())
{
// get the site's web configuration
var siteNameFromServiceModel = "Web"; // 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()
.ToDictionary(e => (string)e["key"], e => (string)e["value"]);
// reconfigure the machine key
var machineKeySection = siteConfig.GetSection("system.web/machineKey");
machineKeySection.SetAttributeValue("validationKey", appSettings["validationKey"]);
machineKeySection.SetAttributeValue("validation", appSettings["validation"]);
machineKeySection.SetAttributeValue("decryptionKey", appSettings["decryptionKey"]);
machineKeySection.SetAttributeValue("decryption", appSettings["decryption"]);
server.CommitChanges();
}
现在,本主题不涉及对 ServiceConfiguration 的更改(假设您从门户修改连接字符串而无需重新部署)。这就是为什么您还需要处理 RoleEnvironment.Changing 事件,以更新此类更改的配置(您也可以在此处阻止实例重启)。