ASP.NET Core Provides Modularity with Middleware Components

| by Pierre-Luc Maheu Follow 4 Followers on Mar 09, 2016. Estimated reading time: 1 minute |

ASP.NET Core introduces middleware as a concept to customize the HTTP pipeline. Middleware are components which are composed together to form a web application. The concept was inspired by OWIN and Katana, which provided similar functionalities in earlier versions of ASP.NET.

A middleware is a component sitting on the HTTP pipeline. Middleware are executed one by one, with the responsability to execute the next middleware in the pipeline. Each middleware may terminate the call chain. For example, the authentication middleware would not execute the next middleware if the authentication process failed. The following image illutrates the execution flow.

Aside from the pre-built middleware included in ASP.NET Core, it is also possible to create new ones. A custom middleware is defined by a class with a method accepting an HttpContext as its first parameter. The method may include additional parameters, which will be resolved by dependency injection. The following class defines a logging middleware:

public class RequestLoggerMiddleware
    private readonly RequestDelegate _next;
    private readonly ILogger _logger;

    public RequestLoggerMiddleware(RequestDelegate next, ILoggerFactory loggerFactory)
        _next = next;
        _logger = loggerFactory.CreateLogger<RequestLoggerMiddleware>();

    public async Task Invoke(HttpContext context)
        _logger.LogInformation("Handling request: " + context.Request.Path);
        await _next.Invoke(context);
        _logger.LogInformation("Finished handling request.");

The middleware, in order to be executed, must be registered in the Configure method of the Startup class.

  public void Configure(IApplicationBuilder app)

An important point to note is that middleware are executed in the order they are added to the pipeline. This means some care must be put into determining the implicit dependencies between middleware. For example, a component using session state but executed before the session middleware would result in a crash.

With the “pay for what you use” philosophy of ASP.NET Core, some applications may benefit from improved performance as only middleware configured explicitly will run. The framework is no longer based on System.Web.dll; components are instead available as NuGet packages. This also means updates are now handled by NuGet, providing the ability to update each middleware separately.

Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread


Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and don't miss out on content that matters to you