如何从Azure Cloud Service的代码中获取服务名称?

时间:2013-05-27 16:08:24

标签: c# .net azure azure-service-runtime

我正在寻找一种方法,使用RoleEnviroment类或类似的东西从C#代码获取云服务的部署名称,这样如果我的服务部署在myservice.cloudapp.net我得到{{1} }。

我该怎么做?

3 个答案:

答案 0 :(得分:5)

Gaurav部分正确。您必须使用Service Management API。请注意您的术语 - 部署名称通常是表示服务代码当前部署的GUID。您正在寻找ServiceName。使用Service Management API,您可以向Get Hosted Service Properties发出请求。响应对象中的属性ServiceName是您要查找的DNS前缀:

  

云服务的名称。此名称是DNS前缀名称和   可用于访问云服务。例如,如果是云   服务名称是MyService,您可以访问云   通过致电:http://MyService.cloudapp.net

进行服务

答案 1 :(得分:3)

您需要使用Service Management REST API来获取云服务名称。这个操作有点复杂!

以下是您需要执行的步骤:

  1. 获取部署ID。这样您就可以从RoleEnvironment获得。
  2. 接下来,您将获取订阅中所有云服务的列表。为此,您需要执行List Hosted Services操作。
  3. 然后,对于每个云服务,您需要获取属性。为此,您需要执行Get Hosted Service Properties。另外,请确保提供embed-detail=true查询字符串参数。
  4. 在您收到的回复中,您需要找到PrivateID属性并将其与您的部署ID匹配。
  5. 我很长一段时间写了一篇博文,其中有一些代码可以让你做这样的事情:http://gauravmantri.com/2012/03/16/programmatically-finding-deployment-slot-from-code-running-in-windows-azure/

答案 2 :(得分:1)

async public Task<List<XDocument>> GetAzureServices()
    {
        String uri = String.Format("https://management.core.windows.net /{0}/services/hostedservices ", _subscriptionid);
        List<XDocument> services = new List<XDocument>();

        HttpClient http = GetHttpClient();

        Stream responseStream = await http.GetStreamAsync(uri);

        if (responseStream != null)
        {
            XDocument xml = XDocument.Load(responseStream);
            var svcs = xml.Root.Descendants(ns + "HostedService");
            foreach (XElement r in svcs)
            {
                XDocument vm = new XDocument(r);
                services.Add(vm);
            }
       }

        return services;
    }

public HttpClient GetHttpClient()
    {
        WebRequestHandler handler = new WebRequestHandler();
        string CertThumbprint = _certthumbprint;
        X509Certificate2 managementCert = FindX509Certificate(CertThumbprint);
        if (managementCert != null)
        {
            handler.ClientCertificates.Add(managementCert);
            HttpClient httpClient = new HttpClient(handler);
            httpClient.DefaultRequestHeaders.Add("x-ms-version", "2012-03-01");
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
            return httpClient;
        }
        return null;
    }
private static X509Certificate2 FindX509Certificate(string thumbprint)
    {
        X509Store certificateStore = null;
        X509Certificate2 certificate = null;

        try
        {
            certificateStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            certificateStore.Open(OpenFlags.ReadOnly);

            var certificates = certificateStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false);
            if (certificates.Count > 0)
            {
                certificate = certificates[0];
            }
        }
        finally
        {
            if (certificateStore != null) certificateStore.Close();
        }

        return certificate;
    }

您需要指定subcriptionId和证书指纹

相关问题