Lifecycle

A lifecycle is a series of components, events and stages that process a request or respond to user interaction. The framework lifecycle  offers extension points and hooks into these components so that you can customize the final output. Lifecycles provide structure and abstraction that makes building complex applications easier. Android apps have an activity lifecycle, so do WordPress and AngularJs frameworks. Understanding the processing pipeline of a framework and knowing how to work with it empowers you as a developer.

The ASP.NET lifecycle has undergone a major overhaul in .Net core. Prior to ASP.Net core , ASP.NET applications were dependent on and were loaded by IIS. ASP.NET core supports OWIN  and the new generation of applications built with ASP.Net core can run on non-windows platforms such as Linux, Mac OS or even within Docker. This cross platform reach meant that the dependency on IIS had to be removed. To understand more about the secret sauce that enables this head over to this post about OWIN, Katana and Kestrel.

ASP.NET Core has been engineered with dependency injection and a modular HTTP middleware pipeline to facilitate application services.

Middleware

Middleware components form the basic building blocks of an ASP.NET core pipeline providing capabilities such as the ability to serve files and route requests. Middleware is a series of components that form the applications request pipeline. Middleware components provide many of the underlying application level infrastructure. Routing, CORS, authentication and caching are implemented using middleware. App specific middleware can also be written. Each middleware component acts on the request as they come in and also on the response as it is sent back. It can choose to act on a request, ignore it or pass it to a specific component. These components are called in the order they are added to the pipeline.

ASP.Net Core does not have HTTP Modules and handlers.  Previously HTTP modules and handlers provided common services such as caching , authorization and request handling. Modules provided application level services ,They provided a hook into application lifecycle events to author reusable services. Handlers were responsible for generating the response. Module and handler execution were driven by application events whereas middleware exaction is dependent on the order in which they are added to the pipeline. Middleware provides the same results as handlers and modules.  The middleware can also integrate with a larger framework like MVC. While middleware executes in the order in which they are added httphandlers execute every time the associated application event is fired and httpmodule is executed exactly once to generate a response.

The ASP.Net core middleware pipeline can be configured using the following 3 methods

  1. Run – short circuits the request and directly generates a response

  2. Use- Supplies an additional parameter to invoke the next middleware component and invoke logic before and after it is called e.g app.UseDeveloperExceptionPage()

  3. Map – Branches to different middleware components based on the incoming request

e.g app.use , app.run

Program and startup are the 2 main classes involved in setup and configuration of MVC application. Program is a low level entry point into the application through Main(). Main calls startup which configures all the middleware components.

Main creates a webhostbuilder and chains a series of methods to build the application. UseKestrel() sets up the kestrel web server which is a cross platform webserver. UseStartup<> method allows us  to specify an application configuration class using a type parameter. This influences details such as how the http pipeline is setup . The build and run methods actually implement the configuration and startup the application.

The startup class has two methods

  1. Configureservices – Configures services that will be used by the application. This is tied into dependency injection making registered services available to controllers and other components.
  2. Configure – Establishes the core HTTP pipeline by registering the Middleware components. Default helper methods like UseStaticFile and UseMVC  which register middle ware components.

A simple example of middleware components are components that enable using static files app.useStaticFiles() adds middleware that enables our application serve static files such as css and JS. This avoids triggering the mvc  request lifecycle thus optimizing it. Another example of a complex and powerfull middleware component is app.useMvcWithDefaultRoutes(). This is the mot common route template used by mvc and the routing middleware component. These are wrapper extension methods on the app builder class that calls app.use middleware internally.

Picture Courtesy – Stephen Di Donato Location – Art Gallery of Ontario, Toronto , Canada

Summary
ASP.Net Core Request Reponse lifecycle
  • Facebook
  • Twitter
  • LinkedIn
  • Google+
  • Buffer
  • reddit
  • Blogger
Article Name
ASP.Net Core Request Reponse lifecycle
Author
Publisher Name
Pradeeploganathan.com
Publisher Logo
  • Facebook
  • Twitter
  • LinkedIn
  • Google+
  • Buffer
  • reddit
  • Blogger