Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Azure InstanceInput endpoint port

Tags:

azure

I'm want my client to communicate with a specific WorkerRole instance, so I'm trying to use InstanceInput endpoints.

My project is based on the example provided in this question: Azure InstanceInput endpoint usage

The problem is that I don't get the external IP address + port for the actual instance, when using RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint; I just get internal address with the local port (e.g. 10.x.x.x:10100). I know that I can get the public IP address via DNS lookup (xxx.cloudapp.net), but I don't have a glue how to get the correct public port for each instance.

One possible solution would be: get the instance number (from RoleEnvironment.CurrentRoleInstance.Id) and add this instance number to the FixedPortRange minimum (e.g. 10106). This would imply that the first instance will always have the port 10106, the second instance always 10107 and so on. This solution seems a bit hacky to me, since I don't know how Windows Azure assigns the instances to the ports.

Is there a better (correct) way to retrieve the public port for each instance?

Question #2: Are there any information about the Azure Compute Emulator supporting InstanceInput endpoints? (As I already mentioned in the comments: It seems that the Azure Compute Emulator currently doesn't support InstanceInputEndpoint).

Second solution (much better):

To get the public port, the porperty PublicIPEndpoint can be used (I don't know why I didn't notice this property in the first place).

Usage: RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].PublicIPEndpoint;

Warning: The IP address in the property is unused (http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleinstanceendpoint.publicipendpoint.aspx).

First solution:

As 'artfulmethod' already mentioned, the REST operation Get Deployment retrieves interesting information about the current deployment. Since I encountered some small annoying 'issues', I'll will provide the code for the REST client here (in case someone else is having a similiar problem):

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
}

The string 'result' contains all the information about the deployment (format of the XML is described in section 'Response Body' @ http://msdn.microsoft.com/en-us/library/windowsazure/ee460804.aspx)

like image 749
Robar Avatar asked Oct 22 '12 08:10

Robar


1 Answers

To get information about your deployments, including the VIPs and public ports for your role instances, use the Get Deployment operation on the Service Management API. The response body includes an InstanceInputList.

like image 59
artfulmethod Avatar answered Nov 15 '22 02:11

artfulmethod