当前位置:网站首页>ASP.NET 6 中间件系列 - 条件中间件
ASP.NET 6 中间件系列 - 条件中间件
2022-04-23 03:02:00 【dotNET跨平台】
这篇文章是 ASP.NET 6 中间件系列文章的第 4 部分。
到目前为止,我们已经介绍了 ASP.NET 6 中间件的基础知识,展示了如何创建自定义中间件类,并讨论了中间件执行顺序的重要性。
在本系列的最后一部分中,我们将展示在管道中有条件地执行中间件的两种方法:
采用 AppSettings.json 文件中的设置,来确定是否要添加中间件到管道中;
通过使用传入请求的数据,有条件地执行已经在管道中的中间件。

基于 AppSettings 的条件中间件
让我们回顾一下,上一篇文章中的TimeLoggingMiddleware类:
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.");
}
}
}
现在,我们只希望在特定的条件(比如,当我们在追踪一个 BUG,或者应用程序运行缓慢等)下将TimeLoggingMiddleware添加到应用程序管道中。
为了有条件地向管道中添加中间件,我们可以在 AppSettings.json 文件中设置一个可以在Program.cs中读取的字段。
在 AppSettings.json 文件中添加一个名为MiddlewareSettings的配置字段,以及一个名为UseTimeLoggingMiddleware的配置项:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"MiddlewareSettings": {
"UseTimeLoggingMiddleware": "true",
}
}
我们还需要一个类来保存这些设置的值。按照约定,类名应该与配置字段名MiddlewareSettings匹配,而属性名应该与配置项名UseTimeLoggingMiddleware匹配:
namespace MiddlewareNET6Demo
{
public class MiddlewareSettings
{
public bool UseTimeLoggingMiddleware { get; set; }
}
}
然后,在 Program.cs 文件中,我们可以读取 AppSettings.json 的那一部分,并将其映射到MiddlewareSettings类的的一个实例上:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddTransient<ILoggingService, LoggingService>();
var app = builder.Build();
var middlewareSettings = builder.Configuration.GetSection("MiddlewareSettings").Get<MiddlewareSettings>();
只有当middlewareSettings实例的UseTimeLoggingMiddleware属性值为true时,我们才能将TimeLoggingMiddleware添加到管道中:
//...
if(middlewareSettings.UseTimeLoggingMiddleware)
app.UseTimeLoggingMiddleware();
//...
通过这种方式,我们可以根据应用程序的设置,来控制哪个中间件在管道中处于活动状态。
基于请求 URL 的条件中间件
另一种方法可能带有欺骗性质,因为中间件总是会被添加到管道中,但是它除了将执行传递给下一个中间件之外,不会做任何其它事情。
假设我们有一个新的中间件类叫做CultureMiddleware:
using System.Globalization;
namespace MiddlewareNET6Demo.Middleware
{
public class CultureMiddleware
{
private readonly RequestDelegate _next;
public CultureMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
var cultureQuery = context.Request.Query["culture"];
if (!string.IsNullOrWhiteSpace(cultureQuery))
{
var culture = new CultureInfo(cultureQuery);
CultureInfo.CurrentCulture = culture;
CultureInfo.CurrentUICulture = culture;
}
await _next(context);
}
}
}
请注意,该中间件仅在传入请求中存在culture请求参数时,才会执行一些实际工作。
如果该参数存在,中间件将应用程序的当前区域设置为传入参数的值。
例如,如果我们提交了以下请求:
http://codeman.tech/post/123?culture=zh-CN
CultureMiddleware会将应用的区域设置为zh-CN,然后再进行其它正常的处理。
如果传入的请求是:
http://codeman.tech/post/123
那么,中间件将会什么都不做,应用程序的区域仍然是默认的。
这种有条件地执行中间件的方法,比 AppSettings.json 解决方案提供了更细粒度的执行控制,但潜在的代价是始终需要将中间件添加到管道中。
以TimeLoggingMiddleware为例:
如果传入的请求包含特定的值,才执行TimeLoggingMiddleware的代码;
在 MVC 应用程序,也许我们只想记录单个控制器的执行时;
在 Razor 应用中,可能只想记录有问题的页面;
在 Web API 中,只是其中一个端点有些慢。
在这些情况下,我们都可以使用这种方法将TimeLoggingMiddleware定位到我们想要记录的内容。
版权声明
本文为[dotNET跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124310719
边栏推荐
- Android 高阶面试必问:全局业务和项目的架构设计与重构
- 《信息系统项目管理师总结》第七章 项目沟通管理
- ROP Emporium x86_ 64 7 ~ 8 questions
- Regular object type conversion tool - Common DOM class
- Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
- It turns out that PID was born in the struggle between Lao wangtou and Lao sky
- Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
- [if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources
- First knowledge of C language ~ branch statements
猜你喜欢

Solve the problem that PowerShell mining occupies 100% of cpu7 in win7

Specific field information of MySQL export table (detailed operation of Navicat client)

L2-006 树的遍历(中后序确定二叉树&层序遍历)

樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統

Error installing Mongo service 'mongodb server' on win10 failed to start

Traversée de l'arbre L2 - 006

HLS / chisel practice CORDIC high performance computing complex square root

Android high-level interview must ask: overall business and project architecture design and reconstruction

【Hcip】OSPF常用的6种LSA详解

Binary tree
随机推荐
Encapsulate components such as pull-down menu based on ele
Opencv reads webcam video and saves it locally
JZ35 replication of complex linked list
tf. keras. layers. Density function
OCR recognition PDF file
Configuring Apache Web services for servers such as Tianyi cloud
How to build an integrated industrial Internet plus hazardous safety production management platform?
Classification of technology selection (2022)
tf. keras. layers. MaxPooling? D function
Jz76 delete duplicate nodes in linked list
樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
The difference between encodeuri and encodeuricomponent
Essential qualities of advanced programmers
【工欲善其事必先利其器】论文编辑及文献管理(Endnote,Latex,JabRef ,overleaf)资源下载及使用指南
JS learning notes
Binary tree
The input of El input input box is invalid, and error in data(): "referenceerror: El is not defined“
Résumé du gestionnaire de projet du système d'information Chapitre VI gestion des ressources humaines du projet
Traversée de l'arbre L2 - 006
HLS / chisel practice CORDIC high performance computing complex square root