当前位置:网站首页>浅聊一下中间件
浅聊一下中间件
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>();
}
}
边栏推荐
- LOGO特训营 第四节 字体设计的重要性
- Introducing QA into the software development lifecycle is the best practice that engineers should follow
- More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
- 剑指 Offer 67. 把字符串转换成整数
- Close system call analysis - Performance Optimization
- 质量体系建设之路的分分合合
- How to reset the password of MySQL root account
- 集群的概述与定义,一看就会
- 啃下大骨头——排序(二)
- MySQL Architecture - user rights and management
猜你喜欢
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
LOGO特训营 第二节 文字与图形的搭配关系
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
LOGO特训营 第三节 首字母创意手法
30余家机构联合发起数字藏品行业倡议,未来会如何前进?
Business is too busy. Is there really no reason to have time for automation?
集群的概述与定义,一看就会
攻防世界 MISC 进阶区 can_has_stdio?
攻防世界 MISC 进阶 glance-50
随机推荐
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
业务太忙,真的是没时间搞自动化理由吗?
Mongodb aggregation operation summary
Attack and defense world misc advanced area ditf
php短视频源码,点赞时会有大拇指动画飘起
Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
LOGO特訓營 第三節 首字母創意手法
Logo special training camp Section V font structure and common design techniques
Introducing QA into the software development lifecycle is the best practice that engineers should follow
SQL中MAX与GREATEST的区别
剑指Offer 68 - II. 二叉树的最近公共祖先
繁華落盡、物是人非:個人站長該何去何從
Introduction and application of bigfilter global transaction anti duplication component
[the 2023 autumn recruitment of MIHA tour] open [the only exclusive internal push code of school recruitment eytuc]
测试必会:BUG的分类及推进解决
UML图记忆技巧
Mysql root 账号如何重置密码
How to manage 15million employees easily?
Concurrent network modular reading notes transfer