February 22, 2022 by Anuraj
AspNetCore ApiGateway Microservices
This post is about what is an API Gateway and how to build an API Gateway in ASP.NET Core with Ocelot. An API gateway is service that sits between an endpoint and backend APIs, transmitting client requests to an appropriate service of an application. It’s an architectural pattern, which was initially created to support microservices. In this post I am building API Gateway using Ocelot. Ocelot is aimed at people using .NET running a micro services / service orientated architecture that need a unified point of entry into their system.
Let’s start the implementation.
First we will create two web api applications - both these services returns some hard coded string values. Here is the first web api -
CustomersController - which returns list of customers.
And here is the second web api -
Next we will create the API Gateway. To do this create an ASP.NET Core empty web application using the command -
dotnet new web -o ApiGateway. Once we create the gateway application, we need to add the reference of
Ocelot nuget package - we can do this using
dotnet add package Ocelot. Now we can modify the
Program.cs file like this.
Next you need to configure your API routes using
configuration.json. Here is the basic configuration which help to send requests from one endpoint to the web api endpoints.
Now run all the three applications and browse the endpoint - https://localhost:7043/api/products - which invokes the
ProductsController class GET action method. And if we browse the endpoint - https://localhost:7043/api/customers - which invokes the
CustomersController GET action method. In the configuration the
UpstreamPathTemplate will be the API Gateway endpoint and API Gateway will transfers the request to the
Due to some strange reason it was not working properly for me. Today I configured it again and it started working. It is an introductory post. I will blog about some common use cases where API Gateway help and how to deploy it in Azure and all in the future.
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub