Overview
In this article we write a important concept of dotnet core that's called Middleware.First of all some questions is raised in mind. Question are below mentioned.
- What is Middleware?
- Why is it required?
- Why we use the middleware? how to increase the performance or decerease the load on the server?
- Can we create the Custom Middleware?
- Where we can use the middleware?
- How to differ the middlware from Filters?
These are some questions raised. one by one discuss all the problem or question in this article.
Introduction of Middleware
First of all we discuss the previous architecture of Request/Response Pipeline before the middleware.
let's understand the from below Image/Picture.
In this Diagram define the all term and also the define the flow of request.
Now See new Patteren or architecture of Request/Response Pipeline.
In the above Image shown. Middleware is Component that's assembled into application pipeline to handle the request/response.
Each middleware called two times.
(a). When user/client hit the request.
(b). When response is ready according to the request.
Each middleware use for single task. that's mean Middleware follow the SOLID prinicple one rules that's called Single Responsiblity Principle.
Middleware are chained way. that's mean one middleware is executed then go for next middleware execution.
* Middleware is terminating terminal because if your request not full fill the requirement of middleware then it will not hit or not go for execution the controller action Method. that's main reason of the middleware down/decerease the load of the server.
Middleware use basic three method.
-
app.use() =>
it's required two parameter for execution.
(a). Context =>that's represent the current request.
(b). RequestDelegate =>that's mean next middlware exexcution reference. -
app.run() =>
it's the terminating pipeline method. that's mean no one middleware is remaining/left for execution. -
app.map() =>
it's use for convention branching pipeline. that's mean request for matching the given request path.
Middlware Ordering
When we talk about the middleware sequence or ordering so first question in your mind why it's required ? and why is it necessary? so my dear friend we talk about more in deatils in below.
Suppose we are working a middleware for handling the user and role based authenitcation system.
Suppose A User have only two option access Dashboard and Report Page but another User B it's having three menu access Dashboard,Report and user control Page. so user A see the User B third page LINK or get the link.
In this we are provide menu access role and user specific so we create a middleware for checking the user role and user'id not having a menu access but they knowing particular menu url or address so they just logged-in with our credentials and paste particular url or address so they can access the menu or web page. so in this scenario it's required ordering of middleware.
0 Comments
thanks for your suggestion and improving quality of the content