Real Life Use Cases of a Middleware in ASP.NET Core

Interesting use cases for middleware that can simplify your architecture.

Sasha Marfut
Geek Culture

--

Photo by Tim van der Kuip on Unsplash

Middleware is one of the fundamental components of ASP.NET Core applications that can be used to implement various functionality. For example, middleware can be used to implement authorization, convert exceptions to responses, measure request execution time, decompress requests, compress responses, and more. But in addition to these common scenarios, there are other interesting ones that we are going to review next.

Multitenancy Middleware

One way to implement multitenancy in web applications is to keep the data of each customer in a separate database instance.

To implement this architecture, a middleware might be a good choice to dynamically resolve the connection string to the appropriate database instance based on the customer identifier or other information provided in the request.

public class MultitenancyMiddleware
{
private readonly RequestDelegate _next;

public MultitenancyMiddleware(RequestDelegate next)
{
_next = next…

--

--

Sasha Marfut
Geek Culture

1 million+ views on Medium | .NET, Design Patterns, Architecture, API