2012-08-01 53 views

回答

0

我不认为你可以让通用提供者从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事件,以更新此类更改的配置(您也可以阻止实例在此处重新启动)。