Posts Tagged ‘.NET Core’

ASP.NET Core Middleware–Part 1

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

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.


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:


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:


.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.

    In the last post we have discussed on how the ASP.NET Core request processing pipeline works. As a next step, let’s take a closer look into how the ASP.NET Core application gets started and initialized.When we create a new ASP.NET Core application e.g. say a MVC application, we can see two files which gets added by default, Program.cs and Startup.cs as shown in the figure below:


    The Program and the Startup class control startup and the configuration of the application runtime. The ASP.NET core application is just a console application and like any other.NET console application and needs a Main method as the entry point of the application.

    The Program class contains the implementation of this static void Main method which is used to create an instance of IWebHost and run the same. IWebHost is one of the key components of the application which contains the application configuration and the ASP.NET Core Web server.

    The Main method performs the initialization of the IWebHost instance and starts it as shown in the figure below:


    The IWebHostBuilder is used to configure the ASP.NET Core Development Server (default is Kestrel) , Logging , IIS Integration (if required) , Content Root etc.We will take a deeper look into the IWebHost / IWebHostBuilder later.

    Now let’s take a look into the Startup class which gets wired into the IWebHost instance as highlighted below:


    UseStartup is a generic extension method on IWebHostBuilder as shown below. But the interesting point to be noted here is the generic type parameter has no constraints TStartup can be any class.


    This is quite interesting as I am more used to having an interface driven strongly typed parameters in this kind of situations. It’s quite obvious that this Startup class needs to work in tandem with the IWebHost based on certain conventions. Now let’s take a look at the code that is in the Startup class by default.


    a) The Configure method is a must have for the Startup class. This is invoked by the runtime host, which creates an instance of IApplicationBuilder and passes it as an agrument.This IApplicationBuilder is used to configure the middleware pipeline by adding suitable components. The other parameter IHostingEnvironment can be omitted if not required. We will get into further details of the IApplicationBuilder interface while exploring the ASP.NET Core middleware pipeline.

    b) The ConfigureServices method is optional.This can be implemented to add additional services to the IServiceCollection.We will get into further details of the IServiceCollection interface while exploring the ASP.NET Core Dependency Injection.

    Now comes the question, is it a good design, making the startup class work by convention rather than having any strongly typed interface. I think it’s fine. It helps to keep things simple. This is the startup code so we can take up the additional overhead of reflection in inspecting the method definitions and then invoking it. Also, the chances of type mismatch or method mismatch is not there because its the startup code used to configure and initialize the application and should not have a widely varying behavior at runtime. This is also more flexible as we can different other parameters in the methods to suit our needs.

    Next comes the question why we have two classes for the application initialization, Program and Startup? The segregation of responsibilities is very clear, the Program class contains initialization code that is used to set up the infrastructure that will rarely change over the lifetime of the project (like ASP.NET Core Web Server, Logging etc.) whereas Startup contains the initialization of features that are required to customize the application’s behavior.

    ASP.NET Web Forms were launched as part of .NET Framework 1.0 in 2002. It was a logical evolution from ASP, moving away from interpreted web pages with dependency on a scripting engine, towards type safe and compiled web pages model with more well structured code. ASP.NET Web Forms offered developers with a event based programming model, which helped to create web applications quickly. But over time, we found certain limitations of this framework in developing and maintaining large web applications.This was mostly due to lesser control on the generated HTML , poor testability and an artificial/complex stateful model as opposed to the stateless nature of the web.

    ASP.NET MVC was released around 2009 which was based on the Model-View-Controller architectural pattern leading to clean separation of concerns, improved testability and more tighter control on HTML. This was more in line with other popular frameworks of the time like Ruby-On-Rails etc.

    However , both ASP.NET Web Forms and ASP.NET MVC were built on the same underlying framework and relied heavily on features provided by System.Web.dll. This assembly is a part of the .NET Framework and explicitly coupled with Windows Web Server i.e. Internet Information Services., IIS. Also, the heavy dependency on this assembly made changes to ASP.NET complex and release cycles slow.

    The key goals of ASP.NET Core was to make the framework cross platform and the overall architecture more modular. So, clean separation of the web server from the web application was key. The figure below shows the key components of the ASP.NET Core request processing pipeline and how this separation is achieved.

    image Every ASP.NET Core application runs within an in-process HTTP Server implementation. This HTTP Server receives the raw HTTP request and converts it to an internal representation by creating a HttpContext object. This object is then used by the web application for further processing.Kestrel is a cross platform web server built on libuv , asynchronous request processing library. This is the default HTTP Server implementation for all ASP.NET Core applications.

    Kestrel is a very basic HTTP server and does not have advanced features like

    • Windows Authentication
    • Port sharing
    • Http Access Logs
    • Process Activation
    • Response Caching etc.

    This is why its recommended to use the ASP.NET Core Web Server with a reverse proxy. A reverse proxy is a web server exposed directly to the Internet and responsible for forwarding the requests to the appropriate web servers serving the requests. A web server like IIS , Nginx or Apache can be easily configured as a reverse proxy working in conjunction with the ASP.NET Core Web Server.

    This model of having a separate reverse proxy and a web server might sound bit cumbersome. But this helps in making the web application framework independent of the operating system and web servers like IIS, Nginx or Apache. This results in a clean separation of concerns with a lightweight HTTP server handling requests / generating responses while the reverse proxy doing rest of the heavy lifting like security related hardening , application start-up etc.

    An ASP.NET Core web application cannot be directly hosted in IIS / Nginx / Apache without a server like Kestrel or any custom ASP.NET Core Http Server implementation. This is because ASP.NET Core is designed to run independent of this servers, ASP.NET Core is not meant to adapt to these servers but these servers needs to be configured to act as a reverse proxy and forward request to the ASP.NET Core Web Server.

    IIS works as a reverse proxy , forwarding requests to ASP.NET Core using the ASP.NET Core Module ( ANCM). This is a native IIS module which hooks in the request pipeline ,activates the ASP.NET Core process and forwards the request to the ASP.NET Core Web Server. The IIS worker process ( w3wp.exe) and ASP.NET Core runs as two separate processes , ANCM is responsible for restarting in case the ASP.NET Core application crashes.

    There is another option for ASP.NET Core Web Server, HTTP.sys.This is Windows only HTTP Server implementation based on HTTP.sys kernel mode driver. However it has some advanced built in features compared to Kestrel like support for Windows Authentication , Port sharing , response caching etc.HTTP.sys kernel driver is a very advanced technology and IIS itself runs on top of it. So this HTTP Server is protected against security attacks etc. and is suitable for running without a reverse proxy for internet facing applications. For intranet applications also this can be good choice given its support for Windows Authentication.