浅析asp .net core 中间件

时间:2022-07-24
本文章向大家介绍浅析asp .net core 中间件,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

正文

什么是中间件呢?

其实中间件这个概念来源于分布式,当然这是一个狭隘的概念了,现在中间件概念就非常广泛了。

官网给出了这样一张图,这张图认为从请求到响应过程中间都是中间件,包括我们认为的路由。

看一段node代码:

var http = require("http");
http.createServer(function(req,res){
   res.writeHead(200,{"Content-type":"text/blain"});
   res.write("Hello NodeJs");
   res.end();
}).listen(8888);

没有学过node 也是ok的,从字面意思就是创建了一个http服务,然后端口是8888。 createServer 可以传入一个方法,中间有两个参数,一个参数是req,另一个参数就是res。 其实就是这么回事,我们写的就是这个这个方法里面的代码,至于,你想拿到req的什么参数,或者给res写入什么信息,这些都是中间件的范畴。

那么问题来了,问什么叫做中间件呢?从语文的层面上理解,那就是分开的,一件一件的。

把这件要执行的这些封装成一个一个模块,那么这些模块,这些可以通过调用next执行下一个模块,同样,如果不调用,那么中间件模块就会中断,因为有时候真的需要中断,比如说权限中间件,

检查到权限不符合直接返回让其跳转到权限页面,剩下的模块也用不到。

用 Use 将多个请求委托链接在一起。next 参数表示管道中的下一个委托。可通过不调用 next 参数使管道短路。
当委托不将请求传递给下一个委托时,它被称为“让请求管道短路”。通常需要短路,因为这样可以避免不必要的工作。

下图为asp .net core 中默认的中间件顺序,当然可以自己修改,但是这是一种推荐做法。

挺合理的一个东西,一开始就填入了异常处理机制。

然后是强制https 转换->重定向->静态资源文件->路由->是否跨域->认证->授权->我们自己自定义需求的。

因为这个endpoint 是不建议我们修改的,当然我们可以修改源码中,毕竟开源了,but not must。

官网中同样给了我们这个endpoint 做了什么,其实就是官方自己封装了一些中间件。

当然我们也可以进行对endpoint自我的调整,因为可能使用不同的模板引擎:

app.UseEndpoints(endpoints =>
{
	endpoints.MapRazorPages();
	endpoints.MapControllerRoute(
		name: "default",
		pattern: "{controller=Home}/{action=Index}/{id?}");
});

从上面得到一个推荐的中间件注册来看,认证和授权在静态文件之后,那么我们知道我们的资源时需要授权来保护的。

那么如何保护静态资源呢?这后面会有专门的一章实践一下,看下是什么原理。

好的,那么看一下我们如何自定义中间件吧,也就是实践一下下面这种图。

Middleware1

public class Middleware1
{
	public readonly RequestDelegate _next;

	public Middleware1(RequestDelegate next)
	{
		_next = next;
	}
	public async Task Invoke(HttpContext context)
	{
		Console.WriteLine("Middleware1");
		await _next(context);
		await context.Response.WriteAsync("Middleware1");
	}
}
public class Middleware2
{
	public readonly RequestDelegate _next;

	public Middleware2(RequestDelegate next)
	{
		_next = next;
	}
	public async Task Invoke(HttpContext context)
	{
		Console.WriteLine("Middleware2");
		await _next(context);
		await context.Response.WriteAsync("Middleware2");
	}
}

Middleware3

public class Middleware3
{
	public readonly RequestDelegate _next;

	public Middleware3(RequestDelegate next)
	{
		_next = next;
	}
	public async Task Invoke(HttpContext context)
	{
		Console.WriteLine("Middleware3");
		await _next(context);
		await context.Response.WriteAsync("Middleware3");
	}
}

扩展方法:

public static class Middleware1MiddlewareExtensions
{
	public static IApplicationBuilder UserMiddleware1(this IApplicationBuilder builder)
	{
		return builder.UseMiddleware<Middleware1>();
	}

	public static IApplicationBuilder UserMiddleware2(this IApplicationBuilder builder)
	{
		return builder.UseMiddleware<Middleware2>();
	}

	public static IApplicationBuilder UserMiddleware3(this IApplicationBuilder builder)
	{
		return builder.UseMiddleware<Middleware3>();
	}
}

加入中间件队列:

app.UserMiddleware1();
app.UserMiddleware2();
app.UserMiddleware3();

request 顺序

response 顺序

这其实就是一种职责链模式,每个中间件确定是否该需求是否要下一级处理,同样会产生一个处理回调。