Middleware Components In ASP.NET Core

This article will help you understand what is middleware in ASP.NET Core applications and how they really work. It will also help you learn how you can create your own middleware and plug it in-between.

So, to start, let's see,
 
What is a middleware (in context of ASP.NET Core)
 
The definition of "Middleware" varies widely depending on its context, but in relation to ASP.NET 5, the definition provided by the OWIN specification seems closest: "Pass through components that form a pipeline between a server and application to inspect, route, or modify request and response messages for a specific purpose."
 
Middleware are application components which are assembled into an application pipeline to handle requests and responses, and collectively known as middleware. Each component can choose whether to pass the request on to the next component in the pipeline, and can perform certain operations  before and after the next component in the pipeline. Request delegates are used to build this request pipeline, which are then used to handle each HTTP request to your application.
 
ASP.NET 5 has integrated support for middleware. Configure method in Startup class allows you wire up any middleware of your choice.
Let me show you what that exactly means:



Each component has a job to perform when the component receives a request. Each component can perform some work and optionally pass the request to the next component. Each HTTP transaction can visit each piece of middleware once on the way in, and once on the way out. It effectively replaces the functionality that was formerly covered by things like HttpModules and HttpHandlers.
 
Request delegates are configured using Run, Map, and Use extension methods on the IApplicationBuilder type that is passed into the Configure method in the Startup class. An individual request delegate can be specified in-line as an anonymous method, or it can be defined in a reusable class. These reusable classes are middleware, or middleware components. Each middleware component in the request pipeline is responsible for invoking the next component in the chain, or choosing to short-circuit the chain if appropriate.
 
PS:

A Middleware component, in an ASP.NET Core project, is a class like any other. The difference is that the Middleware component needs to have a private property of type RequestDelegate,
 
Let's see how you create your own Middleware. If you're using Visual Studio and have ASP.NET Core installed, there comes a template pre-installed. You're free to write the class in any of your favorite editors otherwise. 
 
You get this ready to plug-in middleware component:
 
 
What all make a middleware component
  • Middleware component needs to have a private property of type RequestDelegate
  • The _next property represents a delegate for the next component in the pipeline
  • Each component must also implement an async task called Invoke, this is where we put our logic of the middleware.
You can add a middleware component just by using another lambda expression inside app.Use() :
 
 
You can use app.Use() with lambda expressions as many times as you want, you just have to await the next middleware in the pipeline and that's it.
 
Here's another middleware component added just to add timestamp in header in Configure method in Startup class:

 
and the server starts giving expected output: 

 
 
 
Hey, did you notice that I didn't await context.Response.Headers.Add()? Yes, I didn't. Because there's no async method to add headers. :)
 
Separating this in a middleware class makes it just clean. So, I moved this code in a middleware class, ServerTime.cs:

 
 
Use app.UseMiddleware to add this middleware component in request pipeline  :
 
 
 
That's all for now. Hope you enjoyed reading.
 
Please share your valuable feedback in the comments section!
Read more articles on ASP.NET Core:

Up Next
    Ebook Download
    View all
    Learn
    View all