title: Getting Azure InstanceInput endpoint port
body: <p>I'm want my client to communicate with a specific WorkerRole instance, so I'm trying to use  InstanceInput endpoints.</p>
  
  <p>My project is based on the example provided in this question: <a href="https://stackoverflow.com/questions/11201660/azure-instanceinput-endpoint-usage">Azure InstanceInput endpoint usage</a> </p>
  
  <p>The problem is that I don't get the external IP address + port for the actual instance, when using <code>RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].IPEndpoint;</code> 
  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.</p>
  
  <p>One possible solution would be: get the instance number (from <code>RoleEnvironment.CurrentRoleInstance.Id</code>) and add this instance number to the <code>FixedPortRange</code> minimum (e.g. 10106). This would imply that the first instance will <strong>always</strong> have the port 10106, the second instance <strong>always</strong> 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.</p>
  
  <p>Is there a better (correct) way to retrieve the public port for each instance?</p>
  
  <p>Question #2:
  Are there any information about the Azure Compute Emulator supporting InstanceInput endpoints? (As I already mentioned in the <a href="https://stackoverflow.com/questions/11201660/azure-instanceinput-endpoint-usage#comment17559730_11201660">comments</a>: It seems that the Azure Compute Emulator currently doesn't support InstanceInputEndpoint).</p>
  
  <p><strong>Second solution (much better)</strong>:</p>
  
  <p>To get the public port, the porperty <code>PublicIPEndpoint</code> can be used (I don't know why I didn't notice this property in the first place).</p>
  
  <p><em>Usage</em>: <code>RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["Endpoint1"].PublicIPEndpoint;</code></p>
  
  <p><em>Warning</em>:
  The IP address in the property is unused (<a href="http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleinstanceendpoint.publicipendpoint.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.serviceruntime.roleinstanceendpoint.publicipendpoint.aspx</a>). </p>
  
  <p><strong>First solution</strong>:</p>
  
  <p>As 'artfulmethod' already mentioned, the REST operation <code>Get Deployment</code> 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):</p>
  
  <pre><code>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/&lt;subscription-id&gt;/services/hostedservices/&lt;service-name&gt;/deployments/&lt;deployment-name&gt;"; // replace &lt;xxx&gt; 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
  }
  </code></pre>
  
  <p>The string 'result' contains all the information about the deployment (format of the XML is described in section 'Response Body' @ <a href="http://msdn.microsoft.com/en-us/library/windowsazure/ee460804.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/windowsazure/ee460804.aspx</a>)</p>