为了账号安全,请及时绑定邮箱和手机立即绑定

ASP.NET Core 2.2 基础知识(二) 中间件

标签:
C#

中间件是一种装配到应用管道以处理请求和相应的软件.每个软件都可以:

1.选择是否将请求传递到管道中的下一个组件;

2.可在调用管道中的下一个组件前后执行工作.

管道由 IApplicationBuilder 创建:

每个委托都可以在下一个委托前后执行操作,.此外,委托还可以决定不将请求传递给下一个委托,这就是对请求管道进行短路.通常需要短路,是因为这样可以避免不必要的工作.比如:

1.静态文件中间件可以返回静态文件请求并使管道的其余部分短路;

2.现在管道中调用异常处理委托,以便他们可以捕获在管道的后期阶段所发生的异常.

委托的添加方式一共有3种:

1.Run

该方法的XML注释是这样写的:

Adds a terminal middleware delegate to the application's request pipeline.向应用程序请求管道添加一个终端中间件.

通俗来讲,意思就是该方法添加的委托,会使"请求管道短路",不管该委托是否提前响应都会短路.比如下面代码中标红的部分,不管有没有这一句代码,下面的所有代码都不会执行.

复制代码

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.Run(async context =>
            {                await context.Response.WriteAsync("Hello World!");
            });            //下面的都不会执行了,因为上面的委托已经终止了管道,或者说:"已经让管道短路了"          ...  
        }

复制代码

 

2.Use

该方法的XML注释是这样写的:

Adds a middleware delegate defined in-line to the application's request pipeline.和上面的 Run 方法相比,少了"terminal".意义就已经很明显了.

复制代码

                //用 Use 将多个请求委托链接在一起. next 参数表示管道中的下一个委托,可通过不调用 next 参数使管道短路.                //通常可在下一个委托前后执行操作,如以下示例
                app.Use(async (context, next) =>
                {                    var name = context.Request.Query["name"];                    if (!string.IsNullOrWhiteSpace(name))
                    {                        await context.Response.WriteAsync($"hello world , {name}");
                    }                    await next.Invoke();
                });

复制代码

请求一:

请求二:

 

3.Map

根据给定请求路径的匹配项来创建请求分支.如果请求路径以给定的路径开头,则执行分支,如红色部分代码

复制代码

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {            //Map            app.Map(new PathString("/map1"), MapTest1);
            app.Map("/map2", MapTest2);
            app.MapWhen(context => !string.IsNullOrWhiteSpace(context.Request.Query["name"]), MapTest3);            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }            else
            {
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseMvc();
        }        public void MapTest1(IApplicationBuilder app)
        {
            app.Run(async context => { await context.Response.WriteAsync("this is maptest1"); });
        }

        public void MapTest2(IApplicationBuilder app)
        {
            app.Run(async context => { await context.Response.WriteAsync("this is maptest2"); });
        }

        public void MapTest3(IApplicationBuilder app)
        {
            app.Run(async context => { await context.Response.WriteAsync("this is maptest3"); });
        }

复制代码

 

另外,Map 支持嵌套 :  app.Map("/map2", builder => { builder.Map("/map22", MapTest22); });

封装中间件

在实际运用过程中,我们通常将中间件封装在类中,然后通过扩展方法公开出来.方式有两种:

一.启动时构造

1.自定义中间件

复制代码

    public class MyMiddleware
    {        private readonly RequestDelegate _next;        public MyMiddleware(RequestDelegate next)
        {
            _next = next;
        }        //方法名必须是 Invoke 或者 InvokeAsync
        public async Task InvokeAsync(HttpContext context)
        {            var name = context.Request.Query["name"];            if (!string.IsNullOrWhiteSpace(name))
            {                await context.Response.WriteAsync($"hello world,{name}");
            }            else
            {                await _next(context);
            }
        }
    }

复制代码

2.通过扩展方法公开

复制代码

    public static class MyMiddlewareExtensions
    {        public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder app)
        {            return app.UseMiddleware<MyMiddleware>();
        }
    }

复制代码

3.调用自定义的中间件.

复制代码

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }            else
            {
                app.UseHsts();
            }            //调用自制中间件            app.UseMyMiddleware();

            app.UseHttpsRedirection();
            app.UseMvc();
        }

复制代码

 

这种方式编写的中间件,是在web应用启动时构造的,而不是按请求构造的,因此相当于单例.

所以,如果想正确使用中间件依赖项的生存期,则需要将这些依赖项添加到 Invoke 或者 InvokeAsync 方法的入参里面,如:

    public class Person
    {        public string Name { get; set; }
    }

复制代码

    public class Startup
    {
        ...other codes    
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);            //services.AddSingleton(new Person() { Name = "admin" });
            //services.AddTransient<Person>();
            services.AddScoped<Person>();
        }
        ...other codes
    }

复制代码

 

复制代码

        //方法名必须是 Invoke 或者 InvokeAsync
        public async Task InvokeAsync(HttpContext context, Person person)
        {            var name = context.Request.Query["name"];            if (!string.IsNullOrWhiteSpace(name))
            {                await context.Response.WriteAsync($"hello world,{name},the person`s hashcode is {person.GetHashCode()}");
            }            else
            {                await context.Response.WriteAsync($"hello world,{person.Name},the person`s hashcode is {person.GetHashCode()}");
            }
        }

复制代码

 

 二.按请求激活

该方式需要自定义中间件实现 IMiddleware 接口.

复制代码

    public class MyMiddleware : IMiddleware
    {        private readonly Person _person;        public MyMiddleware(Person person)
        {
            _person = person;
        }        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {            var name = context.Request.Query["name"];            if (!string.IsNullOrWhiteSpace(name))
            {                await context.Response.WriteAsync($" {name} , hello ! the model`s hashcode is {this.GetHashCode()}");
            }            else
            {                await context.Response.WriteAsync($" {_person.Name} hello ! the model`s hashcode is {this.GetHashCode()}");
            }
        }
    }

复制代码

 

扩展方法的代码没变:

复制代码

    public static class MyMiddlewareExtensions
    {        public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder app)
        {            return app.UseMiddleware<MyMiddleware>();
        }
    }

复制代码

 

调用自制的中间件:

复制代码

    public class Startup
    {        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);            //将中间件对象按我们需要的生存期注入到容器中.            //services.AddTransient<MyMiddleware>();
            //services.AddScoped<MyMiddleware>();
            services.AddSingleton<MyMiddleware>();

        services.AddSingleton(new Person { Name = "admin" });
        }        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {            //注册我们的中间件            app.UseMyMiddleware();

            app.UseHttpsRedirection();
            app.UseMvc();
        }
    }

复制代码

原文出处:https://www.cnblogs.com/refuge/p/10207524.html  

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消