当前位置:网站首页>ASP. Net 6 middleware series - execution sequence
ASP. Net 6 middleware series - execution sequence
2022-04-23 03:06:00 【Dotnet cross platform】

This article is ASP.NET 6 The first of a series of middleware articles 3 part , You can also read The first 1 part and The first 2 part .
The pipeline we create through middleware has execution order , The execution order is the same as the addition order of middleware , Next, let's discuss why there is an execution order , And the importance of it .
The sample project
stay GitHub You can get the code involved in this article on :
https://github.com/zilor-net/ASPNET6Middleware/tree/Part3
Execution order
In this series of The first 1 part in , Middleware constitutes a pipeline , The middleware in the pipeline is executed in a certain order , As shown in the figure below :

The requests pass through the middleware in sequence , The responses are returned in the reverse order .
In the previous article , We have defined two middleware classes :
LoggingMiddlewareUsed to record requests / Response log ;SimpleResponseMiddlewareUsed to interrupt a pipe , Return response .
In this article , We still use LoggingMiddleware For example :
app.UseLoggingMiddleware();
Add delay
Let's create a new middleware class , be called IntentionalDelayMiddleware, It looks something like this :
namespace MiddlewareNET6Demo.Middleware
{
public class IntentionalDelayMiddleware
{
private readonly RequestDelegate _next;
public IntentionalDelayMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
await Task.Delay(100);
await _next(context);
await Task.Delay(100);
}
}
}
This middleware will wait for both incoming requests and outgoing responses 100ms, The total waiting time is 200ms.
Of course , Under the actual situation , We won't do that .
ad locum ,IntentionalDelayMiddleware It just represents some kind of undefined middleware , It takes a predictable time to execute .
We need to add a... To the pipeline IntentionalDelayMiddleware Example . The problem lies in , We are in LoggingMiddleware Add it before or after ?
In fact, in this case , This question may not be important , Because these two middleware will not interact , And don't deal with the same thing .
In this example , Let us in LoggingMiddleware Then add IntentionalDelayMiddleware:
app.UseLoggingMiddleware();
app.UseIntentionalDelayMiddleware();
If you run the application now , We may not find significant differences , because 200 Milliseconds is pretty fast .
Add execution time
In order to monitor the time consumed by each request , We often need to record the execution time of each request to our system .
This requirement is very simple for middleware , We can use .NET Provided Stopwatch Class and 2 Created in this article LoggingService To achieve .
Here's what's called TimeLoggingMiddleware Middleware class :
using MiddlewareNET6Demo.Logging;
using System.Diagnostics;
namespace MiddlewareNET6Demo.Middleware
{
public class TimeLoggingMiddleware
{
private readonly RequestDelegate _next;
private readonly ILoggingService _logger;
public TimeLoggingMiddleware(RequestDelegate next,
ILoggingService logger)
{
_next = next;
_logger = logger;
}
public async Task InvokeAsync(HttpContext context)
{
Stopwatch watch = new Stopwatch();
watch.Start();
await _next(context);
watch.Stop();
_logger.Log(LogLevel.Information, "Time to execute: " + watch.ElapsedMilliseconds + " milliseconds.");
}
}
}
We need to add it to the pipeline . however , There is still a problem : Where should we add it ?
If we were to TimeLoggingMiddleware Add to IntentionalDelayMiddleware Before , Then the delay caused by the latter will be included in the range measured by the former .
If we were to TimeLoggingMiddleware Add to IntentionalDelayMiddleware after , Then the delay caused by the latter will not be included in the range measured by the former .
Let's look at the pipe :
app.UseHttpsRedirection();
app.UseStaticFiles();
// If there is any delay in the middleware , Then the delay will not be included in the time log .
app.UseLoggingMiddleware();
// Time recording middleware
app.UseTimeLoggingMiddleware();
// Delay middleware .
// here , The delay is included in the time log .
app.UseIntentionalDelayMiddleware();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
In this Program.cs In file , Which position is more suitable for TimeLoggingMiddleware? The answer depends on a few questions :
Does the time log need to include execution time such as invalid Authorization ? If it is , Then you must call
app.UseAuthorization()Before , placeTimeLoggingMiddleware.Routing calls take very little time , But you can measure . Are we going to include it ? If it is , You have to call
app.UseRouting()Before , placeTimeLoggingMiddleware.
Like most real-world problems , There is no clear answer to this question .
If there are no clear instructions , Then it is ultimately up to the developer to make a decision according to the specific situation of the system .
It should be noted that :
app.UseIntentionalDelayMiddleware();
app.UseTimeLoggingMiddleware();
These two are totally different :
app.UseTimeLoggingMiddleware();
app.UseIntentionalDelayMiddleware();
This is an example of why the order of Middleware in the pipeline is very important .
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301333959.html
边栏推荐
- Blazor University (11) component - replace attributes of subcomponents
- [software testing] understand the basic knowledge of software testing
- C# 11 的这个新特性,我愿称之最强!
- 最通俗易懂的依赖注入之服务容器与作用域
- tf. keras. layers. MaxPooling? D function
- 編碼電機PID調試(速度環|比特置環|跟隨)
- Using stack to solve the problem of "mini parser"
- Numpy append function
- 微软是如何解决 PC 端程序多开问题的——内部实现
- Numpy stack function
猜你喜欢

Recursion - outputs continuously increasing numbers

C read / write binary file

Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)

svg标签中利用<polygon/>循环数组绘制多边形

由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。

Opencv fills the rectangle with a transparent color

Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)

Vs code setting line feed

C# WPF UI框架MahApps切换主题

最通俗易懂的依赖注入与控制反转
随机推荐
再战leetcode (290.单词规律)
Introduction and use of openfeign component
Typescript Learning Guide
Introduction to ACM [TSP problem]
微软是如何解决 PC 端程序多开问题的
tf. keras. layers. Timedistributed function
tf. keras. layers. Density function
If the deep replication of objects is realized through C #?
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
Judge whether there is a leap year in the given year
Opencv combines multiple pictures into video
.NET点滴:说说Middleware构造中获取不到Scoped服务的问题
Wepy learning record
使用split来解决“最常见的单词”问题
tf. keras. layers. Inputlayer function
Use split to solve the "most common words" problem
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
Load view Caton
Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
【鉴权/授权】自定义一个身份认证Handler