我正在寻找一种方法来使用RoleEnviroment
类或类似的东西从 C# 代码中获取云服务的部署名称,这样如果我的服务部署myservice.cloudapp.net
在myservice
.
我怎样才能做到这一点?
我正在寻找一种方法来使用RoleEnviroment
类或类似的东西从 C# 代码中获取云服务的部署名称,这样如果我的服务部署myservice.cloudapp.net
在myservice
.
我怎样才能做到这一点?
高拉夫是部分正确的。您将不得不使用服务管理 API。请注意您的术语 - 部署名称通常是一个 GUID,表示服务代码的当前部署。您正在寻找 ServiceName。使用服务管理 API,您可以向Get Hosted Service Properties
. 响应对象中的属性 ServiceName 是您要查找的 DNS 前缀:
云服务的名称。此名称为 DNS 前缀名称,可用于访问云服务。例如,如果云服务名称是 MyService,您可以通过调用以下方式访问云服务:http: //MyService.cloudapp.net
您需要使用Service Management REST API
来获取云服务名称。操作有点复杂!
以下是您需要执行的步骤:
List Hosted Services
操作。Get Hosted Service Properties
. 还要确保您提供embed-detail=true
查询字符串参数。PrivateID
属性并将其与您的部署 ID 匹配。我很久以前写了一篇博客文章,其中有一些代码可以让你做这样的事情:http: //gauravmantri.com/2012/03/16/programmatically-finding-deployment-slot-from-code-running-in- windows-天蓝色/ .
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 和证书指纹