Azure,云服务,使用.NET扩展

时间:2017-03-05 13:11:51

标签: .net azure

如何使用Microsoft.Azure.Management.Compute更改Cloud Service上的实例编号? 或者任何其他.NET方式来做到这一点?

2 个答案:

答案 0 :(得分:0)

不是你的问题的答案(因为我没有使用过这个库),但我会尝试给你一个大概(希望它可以帮助你在库中找到正确的方法)。

您可能已经知道,Cloud Service Role的实例数存储在配置文件(* .cscfg)中,该文件是XML文件。扩展Cloud Service涉及更改此配置文件。

您需要做的是首先获取此配置文件。获得此配置文件后,您可以通过读取文件的内容来创建XML文档。

接下来,您需要在此XML文档中找到您希望扩展的所需角色的<Instances>节点。您可以将此节点的count属性值更新为所需的实例计数。

一旦这样做,您将需要应用此更改的配置。被调用的REST API操作是Change Deployment Configuration。这将启动您的Cloud Service扩展。

答案 1 :(得分: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