Thursday, 21 January 2021

Part 6: Middleware in Asp.Net Core

#10 minutes of reading


Middleware is nothing but piece of software component which is created to process Http request and response. Asp.Net Core request pipeline contains sequence of middleware to process http request and response.

Middleware is part of Asp.Net application life cycle, lets understand with below screen.

 


Request came from internet and hit IIS/Apache/NGINX web server which is external web server or proxy server. External web server forward http incoming request to kestrel and then forward the same request to middleware. Middleware sequentially checks incoming request. If middleware 1 cannot process it will park to next middleware till the request does not satisfy with any one of the middleware. Middleware can also short circuit incoming request. Suppose incoming request satisfy in 2nd middleware then 2nd middleware may terminate request and return response. Short circuit of request is nothing, but request terminated before executing all middleware in request pipeline.

 

Startup.cs file is place where all configuration related to Http pipeline available. All the request needs to travel from HTTP pipeline. Middleware is piece of software code functional and provided by Core framework. Based on requirement we can attach/add middleware in configure method and what middleware you configured in Configure method that middleware only execute. It’s like Plug-n-Play concept based on requirement add middleware from set of middlwares.


Above screen show life cycle of Out-Of-Process hosting model including two web server external web server and internal web server. Another scenario in Out-Of-Process hosting where Kestrel only main primary web server which handle request from internet directly shown below.


 For In-Process Hosting IIS come into picture as web server which handle http request from internet and no concept of kestrel here. This works only for Winsows OS.


Again, I’m referring the same application created in part 4, click on startup.cs file.


ConfigureServices method used for configuring/adding reference of services like dependency injection, MVC, connection string, ..etc. 

Configure method used to configure middleware based on requirements. Services added in ConfigureServices method and middleware gets added in configure method. In above screen, methods highlighted in Configure method are middleware, press F12 to navigate inside and check header.

 It means when creating application few middleware comes by default shown in above screen of configure method. 

UseEndPoints is last middleware in application. Let’s Short Circuit or terminate http request before UseEndPoints middleware.

Add below highlighted code in Configure method.


Now run application, instead of showing process name on browser, it will show Hello World! text.

Run middleware terminate http pipeline or we can say short circuit.


Do not worry I will show you more middleware near future in coming article and services required to configure for application. For now, just understand the concept and where needs to configure service reference and where middleware component.


Previous                                                                                                                                                Next

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home