Search code examples
azureazure-web-app-serviceazure-hybrid-connections

Can't use localhost for Azure Hybrid Connection's Endpoint Host


It's been about two years since I had to set up a Hybrid Connection for one of my App Services. Last time and this time the use cases are basically the same. I have a Web API hosted on two on-prem boxes. One is the production box and the other is the testing box. Likewise, I have two App Services, one for production and one for testing. The production App Service needs to utilize the Web API on the production on-prem box, and the testing App Service needs to access the Web API on the testing on-prem box.

Last time, I set up a Hybrid Connection for each on-prem box. For both, I used the Endpoint Host "localhost" and the same port number (e.g. 3000). On the testing box, I used Hybrid Connection Manager to set up a connection to the testing Hybrid Connection, and vice versa for production.

The beauty of this is that the codebase that gets deployed to the App Services just calls the on-prem API using http://localhost:3000. Because each App Service uses a different Hybrid Connection, the prod App Service uses the prod on-prem box and the testing App Service uses the testing on-prem box. There's no need to determine the environment and use a different machine name to make the API calls.

Well, that's all fine and dandy, except when I went to replicate this setup today for another API we need to expose, the Azure Portal would not let me use localhost as the Endpoint Host.

Azure Portal

Did they change something, or am I just forgetting how to do this? Am I now forced to use the actual machine's name, and make my codebase detect the environment and make API calls to different hosts based on that? The old ones are still using localhost and work just fine. I'd really prefer to just use localhost if possible.


Solution

  • As explained in another answer, Azure no longer allows the use of localhost in this way. I was able to get past this issue while still keeping just one URL to use in all of my environments. What I did was:

    1. On each machine/environment that I needed to connect to, I edited the hosts file (all of my machines are Windows, but other systems have something similar). I chose a fake "domain" name (e.g. my-hybrid-connection) and set it to point at 127.0.0.1 (which is localhost).
    2. Because these machines are running multiple web sites on IIS, I bound the one this connection is for to a specific port (e.g. 3000).
    3. I had a Service Bus Namespace for each environment. In each of those, I registered a Hybrid Connection and specified the endpoint as my-hybrid-connection:3000.
    4. In my app, I can now make calls to http://my-hybrid-connection:3000. Each environment has its own App Service, and each of those uses the corresponding separate namespace mentioned in step 3. So, regardless of environment, calls to that URL will go to the correct machine.