2012-08-01 51 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事件,以更新此類更改的配置(您也可以阻止實例在此處重新啓動)。