当前位置:首页 > Web开发 > 正文

ASP.NET Core 中间件的几种实现方法

2024-03-31 Web开发

ASP.NET Core 中 HTTP 管道使用中间件组合措置惩罚惩罚的方法,

换句人话来说,

对付写代码的人而言,一切皆中间件.

业务逻辑/数据访谒/等等一切都需要以中间件的方法来泛起.

那么我们必需学会如何实现自界说中间件 这里划重点,必考

这里我们介绍下中间件的几种实现方法...

匿名函数

凡是新建一个空的 ASP.NET Core Web Application,项目名字无所谓啦

在启动类里可以看到这么一句:

// Startup.cs // ... app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); // ...

这就是一个匿名函数实现的中间件,虽然内容对照少.

可以看到通过匿名函数实现的中间件是内嵌在启动类文件中的,因此凡是也叫做内联中间件

接下来,我们通过匿名函数来实现内联中间件,以便加深理解.

新建一个空的 ASP.NET Core Web Application

然后改削启动类代码如下:

// Startup.cs using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using System; namespace WebApplication1 { public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 使用匿名函数实现一个内联中间件 app.Use(async (context, next) => { throw new NotImplementedException("一个使用匿名函数,但未实现具体内容的内联中间件"); }); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } } }

这里我们在 app.Run 之前使用 app.Use 添加一个匿名函数实现的内联中间件,凭据中间件的注册挨次,当倡议请求时,会抛出一个异常 NotImplementedException("一个使用匿名函数,但未实现具体内容的内联中间件")

我们 F5 启动下,看看页面

嗯,切合预期.

我们再来调解下启动类,代码如下:

using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace WebApplication1 { public class Startup { public void ConfigureServices(IServiceCollection services) { } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 使用匿名函数实现一个内联中间件 app.Use(async (context, next) => { // 这里不同错误 request 做任何措置惩罚惩罚,直接挪用下一其中间件 await next.Invoke(); }); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } } }

这里我们在 app.Run 之前使用 app.Use 添加一个匿名函数实现的内联中间件,该中间件没有对 request 做任何措置惩罚惩罚,只是一个空的空间件,凭据中间件的注册挨次,当倡议请求时,页面应该显示 Hello World!.

我们 F5 启动,看看效果

嗯,切合预期.

小我私家感受:匿名函数不是很直不雅观,但是用内联的方法可以快速开始一些开发,不用新建一其中间件类,不用专门想个不一样的名字,小场景下长短常便利实用的

实现接口

通过实现接口 IMiddleware 编写自界说中间件,这是一种强类型的方法,我们需要必需强制凭据接口的界说来实现.

IMiddleware

接口 IMiddleware 界说如下:

using System.Threading.Tasks; namespace Microsoft.AspNetCore.Http { public interface IMiddleware { Task InvokeAsync(HttpContext context, RequestDelegate next); } }

可以看到接口 IMiddleware 的定名空间是 Microsoft.AspNetCore.Http,需要实现的要领是InvokeAsync(),看起来不算太庞大, 嗯,看起来不算太庞大

嗯,从头开始,我们新建一个空的 ASP.NET Core Web Application

然后我们通过实现接口的方法来自界说一其中间件,代码如下:

// 新建类 MyMiddleware.cs using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace WebApplication1 { public class MyMiddleware : IMiddleware { public Task InvokeAsync(HttpContext context, RequestDelegate next) { throw new NotImplementedException(); } } }

凭据上面实现的中间件 MyMiddleware,在执行时应该会抛出 NotImplementedException.

使用接口实现的中间件需要在先在处事容器中注册

// Startup.cs using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; namespace WebApplication1 { public class Startup { public void ConfigureServices(IServiceCollection services) { // 在处事容器中注册自界说中间件 services.AddSingleton<MyMiddleware>(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } // 使用 UseMiddleware() 把自界说中间件添加到管道中 app.UseMiddleware<MyMiddleware>(); app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); }); } } }

然后 F5 启动,页面上可以看到如下功效:

切合我们上面的预期,抛出了一个 NotImplementedException.

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32294.html