Archive for November, 2019

ASP.NET Core Middleware–Part 1

Posted: November 11, 2019 in .NET, C#
Tags:

In one of my earlier post, I have discussed in details about the ASP.NET Core request processing and the key components involved i.e reverse proxy, ASP.NET Core Web Server and the ASP.NET Core infrastructure and application logic.

image

In this post we will take a bit more deep dive into the ASP.NET Core Infrastructure components. The flow of information for processing an incoming request and generating the response is shown in the figure below:

image

The diagram above is not a very generic one but elaborates the ASP.NET Core Infrastructure and Application Logic component into two, Middleware Pipeline and MVC Middleware. Before getting into any further details let’s try to understand what this Middleware Pipeline is all about.

In the above flow, reverse proxy receives the request and forwards the raw HTTP request to the ASP.NET Core Web Server. ASP.NET Core Web Server passes the raw HTTP request and instantiates the HttpContext object. HttpContext is a class that encapsulates all information pertaining to a HTTP request including information related to the subsequently generated HTTP response. For every incoming HTTP request there is one instance of HttpContext created and passed onto the subsequent component , the Middleware Pipeline in this case.

The ASP.NET Core Middleware is a chain of components that forms a pipeline for processing an HTTP request, generating the response and subsequent processing of the response before being passed back to the ASP.NET Core Web Server. To be more precise it does the following:

  • Accepts an incoming HTTP request , analyzes it and does some processing if required and passes the request to the next component.
  • Accepts an outgoing HTTP response, analyzes it and does some processing if required and passes the response to the previous component or, the ASP.NET Core Web Server.
  • Accepts an incoming HTTP request and generates the response and passes the response to the previous component or, the ASP.NET Core Web Server.These type of middleware components which short-circuits the pipeline i.e generates the response and passes back the response to the previous component instead of the next are called terminal middleware because it prevents the next component from processing the request.

These middleware components can perform a wide array of tasks like logging, exception handling, serving static files etc. The most important amongst these in a ASP.NET Core MVC application is the MVC Middleware which processes requests and generates the HTML and API responses. This is a terminal middleware as shown in the above figure. Similarly for requests to serve a static file , the Static File Middleware acts as the terminal middleware as shown below:

image

.NET Core provides a set of middleware components which needs to be configured through code and also has provision to develop custom middleware components.We will take a look at these in the coming posts.