9

RoleEnviromentクラスまたは同様のものを使用して C# コードからクラウド サービスのデプロイ名を取得する方法を探していmyservice.cloudapp.netますmyservice

どうやってやるの?

4

3 に答える 3

5

Gaurav は部分的に正しいです。Service Management API を使用する必要があります。用語に注意してください。展開名は通常、サービス コードの現在の展開を表す GUID です。ServiceName を探しています。Service Management API を使用して、 にリクエストを発行できますGet Hosted Service Properties。応答オブジェクトのプロパティ ServiceName は、探している DNS プレフィックスです。

クラウド サービスの名前。この名前は DNS プレフィックス名であり、クラウド サービスへのアクセスに使用できます。たとえば、クラウド サービス名が MyService の場合、http://MyService.cloudapp.net を呼び出してクラウド サービスにアクセスできます

于 2013-05-27T16:59:58.957 に答える
3

Service Management REST APIクラウド サービス名を取得するには、 を使用する必要があります。操作は少し複雑です!

必要な手順は次のとおりです。

  1. デプロイメント ID を取得します。これは、RoleEnvironment から取得できます。
  2. 次に、サブスクリプション内のすべてのクラウド サービスのリストをフェッチします。このためには、操作を実行する必要がありますList Hosted Services
  3. 次に、クラウド サービスごとにプロパティを取得する必要があります。このためには、実行する必要がありますGet Hosted Service Propertiesembed-detail=trueまた、クエリ文字列パラメーターを必ず指定してください。
  4. PrivateID取得する応答で、属性を見つけて、展開 ID と一致させる必要があります。

私は長い間、次のようなことを可能にするコードを含むブログ投稿を書きました: http://gauravmantri.com/2012/03/16/programmatically-finding-deployment-slot-from-code-running-in- windows-azure/ .

于 2013-05-27T16:51:29.570 に答える
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 と証明書の拇印を指定する必要があります

于 2015-06-16T12:14:39.510 に答える