当前位置:网站首页>Talk about Middleware
Talk about Middleware
2022-07-04 22:50:00 【InfoQ】
First, let's talk about the basic concepts
Let's take another look at a real case
public class TestMiddleware1
{
private readonly RequestDelegate _next;
public TestMiddleware1(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
AnsiConsole.MarkupLine($"[red] I am the first 1 Middleware , I'm coming -{DateTime.Now}[/]");
await _next(context);
AnsiConsole.MarkupLine($"[red] I am the first 1 Middleware , I stepped down -{DateTime.Now}[/]");
}
}
public static class TestMiddlewareExtensions
{
public static void UseTest(this WebApplication app)
{
app.UseMiddleware<TestMiddleware1>();
}
}
var app = builder.Build();
... Other middleware
app.UseTest();

var app = builder.Build();
app.UseTest();
app.Use(async (context, next) =>
{
AnsiConsole.MarkupLine($"[Yellow] I am the first 2 Middleware , I come from the entry file , I'm coming -{DateTime.Now}[/]");
await next(context);
AnsiConsole.MarkupLine($"[Yellow] I am the first 2 Middleware , I come from the entry file , I stepped down -{DateTime.Now}[/]");
});


public class TestMiddleware1
{
private readonly RequestDelegate _next;
public TestMiddleware1(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
AnsiConsole.MarkupLine($"[red] I am the first 1 Middleware , I'm coming -{DateTime.Now}[/]");
await _next(context);
AnsiConsole.MarkupLine($"[red] I am the first 1 Middleware , I stepped down -{DateTime.Now}[/]");
}
}
public class TestMiddleware2
{
private readonly RequestDelegate _next;
public TestMiddleware2(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
AnsiConsole.MarkupLine($"[green] I am the first 2 Middleware , I'm coming -{DateTime.Now}[/]");
await _next(context);
AnsiConsole.MarkupLine($"[green] I am the first 2 Middleware , I stepped down -{DateTime.Now}[/]");
}
}
public class TestMiddleware3
{
private readonly RequestDelegate _next;
public TestMiddleware3(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
AnsiConsole.MarkupLine($"[blue] I am the first 3 Middleware , I'm coming -{DateTime.Now}[/]");
await _next(context);
AnsiConsole.MarkupLine($"[blue] I am the first 3 Middleware , I stepped down -{DateTime.Now}[/]");
}
}
public static class TestMiddlewareExtensions
{
public static void UseTest(this WebApplication app)
{
app.UseMiddleware<TestMiddleware1>();
app.UseMiddleware<TestMiddleware2>();
app.UseMiddleware<TestMiddleware3>();
}
}

边栏推荐
- Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
- 繁華落盡、物是人非:個人站長該何去何從
- 剑指Offer 68 - II. 二叉树的最近公共祖先
- SPSS installation and activation tutorial (including network disk link)
- 堆排序代码详解
- 攻防世界 MISC 进阶区 Ditf
- How to manage 15million employees easily?
- Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
- UML图记忆技巧
- 蓝队攻防演练中的三段作战
猜你喜欢
Sobel filter
Erik baleog and Olaf, advanced area of misc in the attack and defense world
Redis的持久化机制
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
Serial port data frame
蓝队攻防演练中的三段作战
攻防世界 MISC 进阶区 hong
LOGO特训营 第三节 首字母创意手法
On-off and on-off of quality system construction
Unity vscode emmylua configuration error resolution
随机推荐
Unity vscode emmylua configuration error resolution
Logo special training camp section II collocation relationship between words and graphics
【机器学习】手写数字识别
攻防世界 MISC 进阶区 hong
攻防世界 MISC 进阶区 Ditf
Logo Camp d'entraînement section 3 techniques créatives initiales
UML diagram memory skills
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
Attack and defense world misc advanced area Hong
堆排序代码详解
9 - 类
Logo special training camp section 1 Identification logo and logo design ideas
攻防世界 MISC 进阶 glance-50
面试必备 LeetCode 链表算法题汇总,全程干货!
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
The overview and definition of clusters can be seen at a glance
Shell script implements application service log warehousing MySQL
模拟摇杆控制舵机
Sword finger offer 67 Convert a string to an integer
Attack and defense world misc advanced area can_ has_ stdio?