Saturday, May 2, 2020

Hybrid Connection for an Azure App Service

In this post, let's see how we can set up a Hybrid Connection for an Azure App Service. In simple Hybrid Connection for an Azure App Service allows your App Service to access a service that is not exposed to the internet. 

Better to go by an example, let's say you have an API running in your local machine and you need to consume this API from an Azure-hosted App Service.

I have created a very simple ASP.NET Core Web API application, something like below.
using Microsoft.AspNetCore.Mvc;
 
namespace SampleApi.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HelloController : ControllerBase
    {
        [HttpGet]
        public ActionResult<stringGet()
        {
            return Ok("Hello from localhost.");
        }
    }
}
And this is running locally at https://localhost:44350 and I can invoke like this. https://localhost:44350/hello. I have another ASP.NET Core Web API application that will consume this API.
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
 
namespace AppServiceWithHybridConnection.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ValuesController : ControllerBase
    {
        [HttpGet]
        public async Task<ActionResult<string>> Get()
        {
            using var httpClientHandler = new HttpClientHandler
            {
                ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
            };
            using var httpClient = new HttpClient(httpClientHandler);
            HttpResponseMessage httpResponseMessage = await httpClient.GetAsync("https://localhost:44350/hello");
            string message = await httpResponseMessage.Content.ReadAsStringAsync();
 
            return Ok($"Message: {message}");
        }
    }
}
So if I run both these applications locally, of course, it will work.
Running locally
Now, I have created an Azure App Service with below configurations.
Create Azure Windows App Service
Note: I have created it as Azure Windows App Service. As of today (2nd May, 2020) I am writing this post, Hybrid Connections are only available in Windows.

Now, I have deployed the consumer API to this service, exact same code, and now if I go to https://app-hybridconnection-demo.azurewebsites.net/values, of course, it's not going to work. There is no publicly available endpoint https://localhost:44350/hello which this API is calling.
Cannot connect to localhost
Now comes the interesting part. Now let's set up a Hybrid Connection, so the Azure-hosted app service has access to our locally running API.

First, from inside our Windows App Service, I am going to Networking.
Networking
Now I am clicking on Configure your hybrid connection endpoints.
Configure your hybrid connection endpoints
From the next screen, there are 2 options.
  1. Add hybrid connection
  2. Download connection manager
Hybrid Connections
I have clicked on Add hybrid connection and from the next page, I am clicking on Create new hybrid connection.
Create new hybrid connection
Here I am configuring the connection. The important thing here is providing the Endpoint Host and Endpoint Port. I have given the local API information.
Create new hybrid connection
Now I am clicking on Create. Once is created, it will be displayed.
Hybrid Connection Created
When you click on the hybrid connection, you can see the properties and copy the GATEWAY CONNECTION STRING.

All good up to this point. Now let's Download connection manager and install. It's a very straightforward installation and don't think no screenshots/explanation is necessary. Once installed, find and open Hybrid Connection Manager UI.
Hybrid Connection Manager UI
Here, you can either click on Add a new Hybrid Connection or Enter Manually. Add a new option will take you through the Azure Sign-In process which can be a pain, I prefer to use the manual process.
Add Hybrid Connection From Connection String
From here, I am pasting the connection string I have copied and clicked on Add.
Hybrid Connection Is Connected
You can see it's now connected.

Note: In case you are getting Not Connected, try restarting HybridConnectionManager Service.
HybridConnectionManager 
Now I am going back to my Azure-hosted API endpoint and just refreshing the browser.
App Service Consumes From Locallhost
It's working and our Azure-hosted API is consuming the locally running API. Isn't this great?

Learn more,
   Azure App Service Hybrid Connections

Happy Coding.

Regards,
Jaliya

Update: June 17, 2020

Hybrid Connections for Linux apps is now Generally Available:
https://azure.github.io/AppService/2020/06/17/General-Availability-of-Linux-Hybrid-Connections.html

No comments:

Post a Comment