I want my client to communicate with a specific instance of WorkerRole, so I'm trying to use InstanceInput endpoints.
My project is based on the example presented in this question: Using the Azure InstanceInput endpoint
The problem is that I am not getting the external IP address + port for the actual instance when using RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint; I just get an internal address with a local port (e.g. 10.xxx: 10100). I know that I can get the public IP address through DNS lookup (xxx.cloudapp.net), but I have no glue how to get the correct public port for each instance.
One possible solution would be to get an instance number (from RoleEnvironment.CurrentRoleInstance.Id ) and add that instance number to a minimum of FixedPortRange (e.g. 10106). This would mean that the first instance would always have port 10106, the second instance always 10107, etc. This solution seems a bit hacked to me as I don't know how Windows Azure assigns instances to ports.
Is there a better (correct) way to get a common port for each instance?
Question # 2: Is there information on the Azure Compute Emulator that supports InstanceInput endpoints? (As I mentioned in the comments : It seems that Azure Compute Emulator does not currently support InstanceInputEndpoint).
Second solution (much better) :
To get a public port, you can use porperty PublicIPEndpoint (I donโt know why I did not notice this property in the first place).
Usage: RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].PublicIPEndpoint;
Warning: The IP address in the property is not used ( http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleinstanceendpoint.publicipendpoint.aspx ).
First decision :
As mentioned in the artfulmethod, the REST Get Deployment operation retrieves interesting information about the current deployment. Since I ran into some small annoying โproblemsโ, here I will provide the code for the REST client (in case someone has a similar 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 "result" of the line contains all the information about the deployment (the XML format is described in the section "Response body" @ http://msdn.microsoft.com/en-us/library/windowsazure/ee460804.aspx )