我正在寻找一种方法,使用RoleEnviroment
类或类似的东西从C#代码获取云服务的部署名称,这样如果我的服务部署在myservice.cloudapp.net
我得到{{1} }。
我该怎么做?
答案 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
来获取云服务名称。这个操作有点复杂!
以下是您需要执行的步骤:
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-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和证书指纹