简体   繁体   English

ASP.NET通用提供程序+ Azure CSCFG

[英]ASP.NET Universal Providers + Azure CSCFG

Following Hanselman's post about the new ASP.NET Universal Providers: http://www.hanselman.com/blog/IntroducingSystemWebProvidersASPNETUniversalProvidersForSessionMembershipRolesAndUserProfileOnSQLCompactAndSQLAzure.aspx 紧随Hanselman关于新ASP.NET通用提供程序的帖子之后: http : //www.hanselman.com/blog/IntroducingSystemWebProvidersASPNETUniversalProvidersForSessionMembershipRolesAndUserProfileOnSQLCompactAndSQLAzure.aspx

How would you configue it to read the connection string for the CSCFG file as opposed to web.config? 您将如何配置它以读取CSCFG文件而不是web.config的连接字符串?

I don't think you can make the Universal Providers read from the ServiceConfiguration (as opposed to the web.config). 我认为您不能使通用提供程序从ServiceConfiguration中读取(与web.config相对)。 But what you can do is modify the web.config with information from the ServiceConfiguration each time you deploy your application OR each time you modify the ServiceConfiguration. 但是,您可以做的是在每次部署应用程序时或每次修改ServiceConfiguration时,使用ServiceConfiguration中的信息来修改web.config。

In your WebRole.cs you would first write some code that does this. 在WebRole.cs中,您首先要编写一些执行此操作的代码。 There is a topic on MSDN that kinda explains how you can do this: MSDN上有一个主题可以解释您如何执行此操作:

  • Run in elevated mode 在提升模式下运行
  • Reference %WINDIR%\\system32\\inetsrv\\Microsoft.Web.Administration.dll 参考%WINDIR%\\ system32 \\ inetsrv \\ Microsoft.Web.Administration.dll
  • Write this in the OnStart method (you will need to change this code): 在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(); } 

Now what this topic doesn't cover are changes to the ServiceConfiguration (say you modify the connection string from the portal without redeploying). 现在,本主题不讨论的是对ServiceConfiguration的更改(例如,您无需重新部署即可从门户网站修改连接字符串)。 That's why you'll also need to handle the RoleEnvironment.Changing event, to update the configuration on such a change (you can also prevent the instance to reboot here). 这就是为什么您还需要处理RoleEnvironment.Changing事件,以根据此类更改来更新配置(您也可以防止实例在此处重新启动)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM