【问题标题】:How can I get the service name from code for a Azure Cloud Service?如何从 Azure 云服务的代码中获取服务名称?
【发布时间】:2013-05-27 16:08:23
【问题描述】:

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

我该怎么做?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

      1. 获取部署 ID。您可以从 RoleEnvironment 获得此信息。
      2. 接下来,您获取订阅中所有云服务的列表。为此,您需要执行List Hosted Services 操作。
      3. 然后,您需要获取每个云服务的属性。为此,您需要执行Get Hosted Service Properties。还要确保提供embed-detail=true 查询字符串参数。
      4. 在您收到的响应中,您需要找到 PrivateID 属性并将其与您的部署 ID 匹配。

      我很久以前写过一篇博文,其中有一些代码可以让你做这样的事情:http://gauravmantri.com/2012/03/16/programmatically-finding-deployment-slot-from-code-running-in-windows-azure/

      【讨论】:

      • 意味着我必须在部署中上传证书才能获得名称?我去看看。
      • @s093294:是的,您必须编写代码来加载该证书并发出 REST 请求。这就像额外的几十行代码。我同意这是矫枉过正。
      【解决方案3】:
      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 和证书指纹

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-16
        • 2014-08-22
        • 1970-01-01
        • 1970-01-01
        • 2014-08-10
        • 1970-01-01
        • 1970-01-01
        • 2020-11-03
        相关资源
        最近更新 更多