0

我有一個帶有Web角色和工作者角色的Windows Azure雲服務。我建立了一個網站,允許我針對雲服務執行各種管理功能(停止/啓動,重啓實例,添加實例,移除實例)。所有的功能都是通過web API執行的。我的問題是,當我添加一個Web角色的實例時,工作者角色會重新啓動。請注意,如果通過Azure門戶添加實例,則不會發生這種情況。代碼在所有其他方面正常運行。 任何想法如何做到這一點,以便只有受影響的角色回收而不是回收所有角色?當添加Web角色實例時,Azure Worker角色重新啓動

我的代碼:

public void AddInstance() 
    { 
     XDocument configDoc = this.GetDeploymentConfiguration(); 
     var ns = configDoc.Root.GetDefaultNamespace(); 

     configDoc.Root 
      .Elements(ns + "Role") 
      .FirstOrDefault(r => r.Attribute("name").Value.ToLower() == this.RoleName.ToLower()) 
      .Element(ns + "Instances") 
      .Attribute("count") 
      .Value = (int.Parse(configDoc.Root 
          .Elements(ns + "Role") 
          .FirstOrDefault(r => r.Attribute("name").Value.ToLower() == this.RoleName.ToLower()) 
          .Element(ns + "Instances") 
          .Attribute("count") 
          .Value) + 1).ToString(); 

     string encodedString = Convert.ToBase64String(Encoding.UTF8.GetBytes(configDoc.ToString())); 
     this.SetDeploymentConfig(encodedString); 
    } 

    public XDocument GetDeploymentConfiguration() 
    { 
     string uri = string.Format(this.servicePropertiesOperationFormat, this.subscriptionID, this.serviceName, "production", ""); 
     ServiceManagementOperation operation = new ServiceManagementOperation(this.thumbprint, this.versionID); 

     var xdoc= operation.Invoke(uri); 
     var myelm = xdoc.Element(wa + "Deployment").Element(wa + "Configuration"); 

     var mystring= Encoding.UTF8.GetString(Convert.FromBase64String(myelm.Value)); 

     return XDocument.Parse(mystring); 
    } 

    public string SetDeploymentConfig(string configurationFile) 
    { 
     string uri = string.Format(this.servicePropertiesOperationFormat, this.subscriptionID, this.serviceName, "production", "/?comp=config"); 
     ServiceManagementOperation operation = new ServiceManagementOperation(this.thumbprint, this.versionID); 
     string payloadString = string.Format(
      @"<?xml version=""1.0"" encoding=""utf-8""?> 
      <ChangeConfiguration xmlns=""http://schemas.microsoft.com/windowsazure""> 
        <Configuration>{0}</Configuration> 
      </ChangeConfiguration>", configurationFile); 

     XDocument payload = XDocument.Parse(payloadString); 
     return operation.Invoke(uri, payload); 
    } 
+0

您的角色是否共享升級域?考慮這裏解釋的詳細信息:https://msdn.microsoft.com/en-us/library/azure/hh472157.aspx – 2015-03-03 07:02:14

回答

0

這不是很直觀,但你必須取消縮放事件,否則將告訴Azure中重新啓動其他實例。將以下行添加到您的RoleEntryPoint文件中的OnStart方法中:

RoleEnvironment.Changing += (sender, args) => { args.Cancel = false; }; 
+0

我已經運行過之前。令我困惑的是,爲什麼當通過Azure門戶縮放web角色或自動縮放時,worker角色不會重新啓動? – 2015-03-04 15:01:19

+0

是的,我也注意到了,並且Azure有時會回收一些角色的實例,而不是全部。對不起,我沒有回答這是爲什麼。我已經在Azure工作了四年多了,並沒有遇到任何解釋。這也讓我感到困惑。但至少,取消Change事件會修復回收事件,儘管事件仍在繼續。 – 2015-03-04 16:28:47