获取Azure InstanceInput端点端口

时间:2012-10-22 08:04:00

标签: azure

我希望我的客户端与特定的WorkerRole实例通信,所以我正在尝试使用InstanceInput端点。

我的项目基于此问题中提供的示例:Azure InstanceInput endpoint usage

问题是,在使用RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint;时,我没有获得实际实例的外部IP地址+端口 我只获得本地端口的内部地址(例如10.x.x.x:10100)。我知道我可以通过DNS查找(xxx.cloudapp.net)获取公共IP地址,但我没有胶水如何为每个实例获取正确的公共端口。

一种可能的解决方案是:获取实例编号(来自RoleEnvironment.CurrentRoleInstance.Id)并将此实例编号添加到最小FixedPortRange(例如10106)。这意味着第一个实例将始终具有端口10106,第二个实例始终 10107,依此类推。这个解决方案对我来说似乎有些苛刻,因为我不知道Windows Azure如何将实例分配给端口。

是否有更好(正确)的方法来检索每个实例的公共端口?

问题#2: 是否有关于支持InstanceInput端点的Azure计算模拟器的任何信息? (正如我在comments中已经提到的:似乎Azure Compute Emulator目前不支持InstanceInputEndpoint。)

第二种解决方案(更好)

要获得公共端口,可以使用porperty PublicIPEndpoint(我不知道为什么我首先没注意到这个属性。)

用法RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].PublicIPEndpoint;

警告的: 该属性中的IP地址未使用(http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleinstanceendpoint.publicipendpoint.aspx)。

第一个解决方案

作为'artfulmethod'已经提到过,REST操作Get Deployment检索有关当前部署的有趣信息。由于我遇到了一些令人烦恼的小问题,我将在这里提供REST客户端的代码(以防其他人遇到类似的问题):

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

string footPrint = "xxx"; // enter the footprint of the certificate you use to upload the deployment (aka Management Certificate)
X509Certificate2Collection certs = 
certificateStore.Certificates.Find(X509FindType.FindByThumbprint, footPrint, false); 
if (certs.Count != 1) {
    // client certificate cannot be found - check footprint
}
string url = "https://management.core.windows.net/<subscription-id>/services/hostedservices/<service-name>/deployments/<deployment-name>"; // replace <xxx> with actual values
try {
  var request = (HttpWebRequest)WebRequest.Create(url);
  request.ClientCertificates.Add(certs[0]);
  request.Headers.Add("x-ms-version", "2012-03-01"); // very important, otherwise you get an HTTP 400 error, specifies in which version the response is formatted
  request.Method = "GET";

  var response = (HttpWebResponse)request.GetResponse(); // get response

  string result = new StreamReader(response.GetResponseStream()).ReadToEnd() // get response body
} catch (Exception ex) {
  // handle error
}

字符串'result'包含有关部署的所有信息(XML的格式在'Response Body'@ http://msdn.microsoft.com/en-us/library/windowsazure/ee460804.aspx一节中描述)

1 个答案:

答案 0 :(得分:1)

要获取有关部署的信息,包括角色实例的VIP和公共端口,请使用Service Management API上的Get Deployment operation。响应正文包含InstanceInputList