BT
x Share your thoughts on trends and content!

ASP.NET Core Provides Modularity with Middleware Components

by Pierre-Luc Maheu on Mar 09, 2016 |

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)
  {
      app.UseMiddleware<RequestLoggerMiddleware>();
  }

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

Relevance
Style

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

Discuss
General Feedback
Bugs
Advertising
Editorial
Marketing
InfoQ.com and all content copyright © 2006-2016 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT

We notice you're using an ad blocker

We understand why you use ad blockers. However to keep InfoQ free we need your support. InfoQ will not provide your data to third parties without individual opt-in consent. We only work with advertisers relevant to our readers. Please consider whitelisting us.