API Gateways with Ocelot

Summary

Azure API Gateways can be expensive. An alternative is to build your own API Gateway with Ocelot. Ocelot is an Open Source API Gateway based on .NET Core.

The Steps to Build the API Gateway are as follows:

  1. Build the API Gateway project in Visual Studio.
  2. Deploy to a Container service like Azure Containers.

I’ll be connecting to Microsoft Dynamics 365 Online simply because I have an environment ready to be used. The API Gateway pattern can be used for any API endpoint.

Build the Visual Studio API Gateway Project

Steps outlined for this are:

  1. Create a new ASP.NET Core Web Application Project
  2. Add Ocelot NuGet package
  3. Configure Ocelot
  4. Change startup.cs to inject Ocelot

Create a new ASP.NET Core Web Application Project

Create a new ASP.Net Core project. For this, I am using an Empty template.

Add Ocelot NuGet package.

Once the project has been created, add the NuGet Ocelot package to the Project.

Configure Ocelot

To configure Ocelot, we’ll have to add a new file called Ocelot.json. The full instructions to configure Ocelot can be found here: https://ocelot.readthedocs.io/en/latest/introduction/gettingstarted.html.

In the example below, I’m going to do a very basic configuration of the Ocelot API Gateway.

Create a new Ocelot.json file with the following content and add this to the project.

{
    "ReRoutes": [
        {
        "DownstreamPathTemplate": "/todos/{id}",
        "DownstreamScheme": "https",
        "DownstreamHostAndPorts": [
            {
                "Host": "jsonplaceholder.typicode.com",
                "Port": 443
            }
        ],
        "UpstreamPathTemplate": "/todos/{id}",
        "UpstreamHttpMethod": [ "Get" ]
        }
    ],
    "GlobalConfiguration": {
        "BaseUrl": "https://localhost:5000"
    }
}

Login to read.

Leave a comment