当前位置:网站首页>浅聊一下中间件
浅聊一下中间件
2022-07-04 22:23:00 【InfoQ】
先浅聊一下基本概念
再浅看一个实际案例
public class TestMiddleware1
{
private readonly RequestDelegate _next;
public TestMiddleware1(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
AnsiConsole.MarkupLine($"[red]我是第1个中间件,我过来了-{DateTime.Now}[/]");
await _next(context);
AnsiConsole.MarkupLine($"[red]我是第1个中间件,我退下了-{DateTime.Now}[/]");
}
}
public static class TestMiddlewareExtensions
{
public static void UseTest(this WebApplication app)
{
app.UseMiddleware<TestMiddleware1>();
}
}
var app = builder.Build();
...其他中间件
app.UseTest();

var app = builder.Build();
app.UseTest();
app.Use(async (context, next) =>
{
AnsiConsole.MarkupLine($"[Yellow]我是第2个中间件,我来自入口文件,我过来了-{DateTime.Now}[/]");
await next(context);
AnsiConsole.MarkupLine($"[Yellow]我是第2个中间件,我来自入口文件,我退下了-{DateTime.Now}[/]");
});


public class TestMiddleware1
{
private readonly RequestDelegate _next;
public TestMiddleware1(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
AnsiConsole.MarkupLine($"[red]我是第1个中间件,我过来了-{DateTime.Now}[/]");
await _next(context);
AnsiConsole.MarkupLine($"[red]我是第1个中间件,我退下了-{DateTime.Now}[/]");
}
}
public class TestMiddleware2
{
private readonly RequestDelegate _next;
public TestMiddleware2(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
AnsiConsole.MarkupLine($"[green]我是第2个中间件,我过来了-{DateTime.Now}[/]");
await _next(context);
AnsiConsole.MarkupLine($"[green]我是第2个中间件,我退下了-{DateTime.Now}[/]");
}
}
public class TestMiddleware3
{
private readonly RequestDelegate _next;
public TestMiddleware3(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
AnsiConsole.MarkupLine($"[blue]我是第3个中间件,我过来了-{DateTime.Now}[/]");
await _next(context);
AnsiConsole.MarkupLine($"[blue]我是第3个中间件,我退下了-{DateTime.Now}[/]");
}
}
public static class TestMiddlewareExtensions
{
public static void UseTest(this WebApplication app)
{
app.UseMiddleware<TestMiddleware1>();
app.UseMiddleware<TestMiddleware2>();
app.UseMiddleware<TestMiddleware3>();
}
}

边栏推荐
- MD5 tool class
- Gnawing down the big bone - sorting (II)
- NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
- 达梦数据凭什么被称为国产数据库“第一股”?
- Logo special training camp section II collocation relationship between words and graphics
- Attack and defense world misc advanced area Hong
- 攻防世界 MISC 进阶 glance-50
- Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
- 集群的概述与定义,一看就会
- How to send a reliable request before closing the page
猜你喜欢
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
LOGO特训营 第三节 首字母创意手法
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { fmt.Pri
业务太忙,真的是没时间搞自动化理由吗?
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
Unity Xiuxian mobile game | Lua dynamic sliding function (specific implementation of three source codes)
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
10 schemes to ensure interface data security
sobel过滤器
页面关闭前,如何发送一个可靠请求
随机推荐
虚拟人产业面临的挑战
Analysis of environmental encryption technology
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
达梦数据凭什么被称为国产数据库“第一股”?
Serial port data frame
页面关闭前,如何发送一个可靠请求
Attack and defense world misc advanced grace-50
MySQL Architecture - user rights and management
Close system call analysis - Performance Optimization
Flask 上下文详解
Breakpoint debugging under vs2019 c release
30余家机构联合发起数字藏品行业倡议,未来会如何前进?
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
Attack and defense world misc advanced area ditf
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { fmt.Pri
攻防世界 MISC 进阶 glance-50
PMO: compare the sample efficiency of 25 molecular optimization methods
Lost in the lock world of MySQL
La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?