Understanding ASP.NET Core Middleware Fundamentals
Middleware in ASP.NET Core is software assembled into an application pipeline to handle requests and responses effectively. It acts as a gateway to decide the flow of requests, allowing control over how requests are processed by various components. Configuration and usage of middleware in applications are vital for controlling the request flow and response handling.
Download Presentation
![](/assets/img/so-down.gif)
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
#05# ASP.NET Core Middleware Fundamentals Design by: TEDU Trainer: Bach Ngoc Toan Website: www.tedu.com.vn Facebook: fb.com/teduchannel Please like videos and subscribe TEDU Channel to following the next video.
What is middleware Middleware is software that is assembled into an application pipeline to handle requests and responses. This means that they are effectively "gateway" classes that can decide whether or not to continue allowing the request to be processed by other Middleware components further down the stream.
Configuring Middleware Adding middleware to an application happens in the Configure method of the startup class for an application. The Configure method is injectable, meaning you can ask for any other services you need, but the one service you ll always need is the IApplicationBuilder service. Most middleware will live in a NuGet package. Each NuGet package will include extension methods for IApplicationBuilder to add a middleware component using a simple method call. Notice the extension methods all start with the word Use.
Configuring Middleware The order that middleware components are added in the Configure method defines the order in which they are invoked on requests, and the reverse order for the response.
Built-in middleware Middleware Description Authentication Provides authentication support. CORS Configures Cross-Origin Resource Sharing. Response Caching Provides support for caching responses. Response Compression Provides support for compressing responses. Routing Defines and constrains request routes. Session Provides support for managing user sessions. Static Files Provides support for serving static files and directory browsing. URL Rewriting Middleware Provides support for rewriting URLs and redirecting requests.
Creating Middleware First, Invoke will receive an HttpContext object with access to the request and the response. You can inspect incoming headers and create outgoing headers. You can read the request body or write into the response. The logic inside Invoke can decide if you want to call the next piece of middleware or handle the response entirely in the current middleware. Note the name Invoke is the method ASP.NET will automatically look for (no interface implementation required), and is injectable.
Run, Map, and Use Run is a convention, and some middleware components may expose Run[Middleware] methods that run at the end of the pipeline. Map branches the request pipeline based on matches of the given request path MapWhen branches the request pipeline based on the result of the given predicate.
Migrating module code to middleware IHttpModule Middleware IHttpHandler Middleware
Advantages The ability to explicitly configure every component of the HTTP processing pipeline makes it easy to know what is happening inside an application. The application is also as lean as possible because we can install only the features an application requires. Middleware is one reason why ASP.NET Core can perform better and use less memory than its predecessors.
Disadvantages Time consuming to move existed HTTP modules to middleware when migrate from older version to .NET Core. Middleware components are highly asynchronous and often follow functional programming idioms. Also, there are no interfaces to guide middleware development as most of the contracts rely on convention instead of the compiler. All of these changes make some developers uncomfortable. Difficulty when find match middleware in Nuget before make decision to create new middleware.
Summary Expect to see middleware play an increasingly important role in the future. Not only will Microsoft and others create more middleware, but also expect the sophistication of middleware to increase. Future middleware will not only continue to replace IIS features like URL re- writing, but also change our application architecture by enabling additional frameworks and the ability to compose new behavior into an application. Don t underestimate the importance of porting existing logic into middleware and the impact of a middleware on an application s behavior.