2017-03-05 59 views

回答

0

Microsoft.Azure.Management.Compute客戶端庫用於在Azure資源管理器下爲虛擬機。爲了管理Azure雲服務,我們可以使用this Management Libraries for .NET(Microsoft.WindowsAzure.Management.Compute)

以下示例代碼在我身邊正常工作,請參閱它。

static void Main(string[] args) 
    { 
     var managementCertThumbprint = "{CertThumbprint}"; 
     var subscriptionid = "{subscriptionid}"; 
     var servicename = "fehancloud"; 

     X509Certificate2 managementCert = FindX509Certificate(managementCertThumbprint); 

     SubscriptionCloudCredentials creds = new CertificateCloudCredentials(subscriptionid, managementCert); 
     ComputeManagementClient cmclient = new ComputeManagementClient(creds); 

     var detailed = cmclient.HostedServices.GetDetailed(servicename); 

     var deployment = detailed.Deployments 
      .First(_ => _.DeploymentSlot == DeploymentSlot.Production); 

     var xConfig = XDocument.Parse(deployment.Configuration); 

     Func<string, XName> n = (name) => XName.Get(name, 
      "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration"); 

     Func<XDocument, string, int> getInstanceCount = (doc, rolename) => 
     { 
      var role = doc.Root.Elements(n("Role")).FirstOrDefault(_ => _.Attribute("name").Value == rolename); 
      if (role == null) return -1; 
      var v = role.Element(n("Instances")).Attribute("count").Value; 
      Console.WriteLine(int.Parse(v)); 
      return int.Parse(v); 
     }; 

     Action<XDocument, string, int> setInstanceCount = (doc, rolename, newInstanceCount) => 
     { 
      if (newInstanceCount < 1) 
      { 
       newInstanceCount = 1; 
      } 

      var role = doc.Root.Elements(n("Role")).FirstOrDefault(_ => _.Attribute("name").Value == rolename); 
      role.Element(n("Instances")).Attribute("count").Value = newInstanceCount.ToString(); 
     }; 

     Action<XDocument, string, int> changeInstanceCount = (doc, rolename, deltaCount) => 
     { 
      int oldCount = getInstanceCount(doc, rolename); 
      var newCount = oldCount + deltaCount; 
      setInstanceCount(doc, rolename, newCount); 
     }; 

     changeInstanceCount(xConfig, "WorkerRole1", 2); 

     var response = cmclient.Deployments.ChangeConfigurationBySlot(
      serviceName: detailed.ServiceName, 
      deploymentSlot: deployment.DeploymentSlot, 
      parameters: new DeploymentChangeConfigurationParameters() 
      { 
       Configuration = xConfig.ToString() 
      }); 
    } 

    private static X509Certificate2 FindX509Certificate(string managementCertThumbprint) 
    { 
     X509Store store = null; 

     try 
     { 
      store = new X509Store(StoreName.My, StoreLocation.CurrentUser); //set StoreLocation based on your specified value 
      store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); 
      X509Certificate2Collection certs = store.Certificates.Find(
       findType: X509FindType.FindByThumbprint, 
       findValue: managementCertThumbprint, 
       validOnly: false); 
      if (certs.Count == 0) 
      { 
       return null; 
      } 

      return certs[0]; 
     } 
     finally 
     { 
      if (store != null) store.Close(); 
     } 
    } 

注意:我上傳了一個管理證書。

packages.config

<?xml version="1.0" encoding="utf-8"?> 
<packages> 
    <package id="Hyak.Common" version="1.0.2" targetFramework="net45" /> 
    <package id="Microsoft.Azure.Common" version="2.0.4" targetFramework="net45" /> 
    <package id="Microsoft.Azure.Common.Dependencies" version="1.0.0" targetFramework="net45" /> 
    <package id="Microsoft.Bcl" version="1.1.9" targetFramework="net45" /> 
    <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net45" /> 
    <package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net45" /> 
    <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.8" targetFramework="net45" /> 
    <package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net45" /> 
    <package id="Microsoft.WindowsAzure.Management.Compute" version="13.0.0" targetFramework="net45" /> 
    <package id="Newtonsoft.Json" version="6.0.4" targetFramework="net45" /> 
</packages> 

後,它執行changeInstanceCount方法,你可以看到新的情況下開始。

enter image description here

0

對於你的問題並非真正的答案(因爲我沒有使用過這個庫),但是我會試着給你一個大概的想法(希望它能幫助你在庫中找到正確的方法)。

您可能已經知道,Cloud Service Role的實例數存儲在XML文件的配置文件(* .cscfg)中。擴展雲服務涉及更改此配置文件。

你需要做的是首先得到這個配置文件。一旦你得到這個配置文件,你就可以通過讀取文件的內容來創建一個XML文檔。

接下來,您需要在此XML文檔中找到<Instances>節點,以獲取您希望擴展的所需角色。您需要將此節點的count屬性的值更新爲所需的實例數。

一旦你這樣做了,你將需要應用這個改變了的配置。被調用的REST API操作是Change Deployment Configuration。這將啓動您的Cloud Service的擴展。

相關問題