IMiddlewareFactory/IMiddleware是中间件激活的扩展点。

UseMiddleware扩展方法检查中间件的已注册类型是否实现IMiddleware。如果是,则使用在容器中注册的IMiddlewareFactory实例来解析IMiddleware实现,而不使用基于约定的中间件激活逻辑。中间件在应用的服务容器中注册为作用域或瞬态服务。

优点:

  • 按客户端请求(作用域服务的注入)激活
  • 让中间件强类型化

IMiddleware按客户端请求(连接)激活,因此作用域服务可以注入到中间件的构造函数中。

IMiddleware

IMiddleware定义应用的请求管道的中间件。InvokeAsync(HttpContext, RequestDelegate)方法处理请求,并返回代表中间件执行的Task。

使用约定激活的中间件:

public class ConventionalMiddleware
{
 private readonly RequestDelegate _next;

 public ConventionalMiddleware(RequestDelegate next)
 {
  _next = next;
 }

 public async Task InvokeAsync(HttpContext context, AppDbContext db)
 {
  var keyValue = context.Request.Query["key"];

  if (!string.IsNullOrWhiteSpace(keyValue))
  {
   db.Add(new Request()
    {
     DT = DateTime.UtcNow, 
     MiddlewareActivation = "ConventionalMiddleware", 
     Value = keyValue
    });

   await db.SaveChangesAsync();
  }

  await _next(context);
 }
}

使用MiddlewareFactory激活的中间件:

public class FactoryActivatedMiddleware : IMiddleware
{
 private readonly AppDbContext _db;

 public FactoryActivatedMiddleware(AppDbContext db)
 {
  _db = db;
 }

 public async Task InvokeAsync(HttpContext context, RequestDelegate next)
 {
  var keyValue = context.Request.Query["key"];

  if (!string.IsNullOrWhiteSpace(keyValue))
  {
   _db.Add(new Request()
    {
     DT = DateTime.UtcNow, 
     MiddlewareActivation = "FactoryActivatedMiddleware", 
     Value = keyValue
    });

   await _db.SaveChangesAsync();
  }

  await next(context);
 }
}

程序会为中间件创建扩展:

public static class MiddlewareExtensions
{
 public static IApplicationBuilder UseConventionalMiddleware(
  this IApplicationBuilder builder)
 {
  return builder.UseMiddleware<ConventionalMiddleware>();
 }

 public static IApplicationBuilder UseFactoryActivatedMiddleware(
  this IApplicationBuilder builder)
 {
  return builder.UseMiddleware<FactoryActivatedMiddleware>();
 }
}

无法通过UseMiddleware将对象传递给工厂激活的中间件:

public static IApplicationBuilder UseFactoryActivatedMiddleware(
 this IApplicationBuilder builder, bool option)
{
 // Passing 'option' as an argument throws a NotSupportedException at runtime.
 return builder.UseMiddleware<FactoryActivatedMiddleware>(option);
}

将工厂激活的中间件添加到Startup.ConfigureServices的内置容器中:

public void ConfigureServices(IServiceCollection services)
{
 services.AddDbContext<AppDbContext>(options =>
  options.UseInMemoryDatabase("InMemoryDb"));

 services.AddTransient<FactoryActivatedMiddleware>();

 services.AddRazorPages();
}

两个中间件均在Startup.Configure的请求处理管道中注册:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
 if (env.IsDevelopment())
 {
  app.UseDeveloperExceptionPage();
 }
 else
 {
  app.UseExceptionHandler("/Error");
 }

 app.UseConventionalMiddleware();
 app.UseFactoryActivatedMiddleware();

 app.UseStaticFiles();
 app.UseRouting();

 app.UseEndpoints(endpoints =>
 {
  endpoints.MapRazorPages();
 });
}

IMiddlewareFactory

IMiddlewareFactory提供中间件的创建方法。中间件工厂实现在容器中注册为作用域服务。

可在Microsoft.AspNetCore.Http包中找到默认的IMiddlewareFactory实现(即MiddlewareFactory)。

广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。