我可以按任何顺序将中间件应用到应用程序吗? [英] Can I apply middleware to the app in any order?

查看:55
本文介绍了我可以按任何顺序将中间件应用到应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#ASP.NET中,中间件应用程序的顺序重要吗?

In C# ASP.NET does the order of middleware application matter?

以下2个代码段:

public class Startup
{
    ...
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        setUpMVCRoutes(app);
        app.UseSwaggerUi("foobar/api", "/foobar/v3/api.json");
        app.UseSwaggerGen("foobar/{apiVersion}/api.json");
        app.UseDefaultFiles();
        app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());
        app.UseStaticFiles();
        app.UseIdentity();
        app.UseCookieAuthentication();
    }
    ...
}

还有这个

public class Startup
{
    ...
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseIdentity();
        app.UseCookieAuthentication();
        app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());
        app.UseDefaultFiles();
        app.UseStaticFiles();
        setUpMVCRoutes(app);
        app.UseSwaggerGen("foobar/{apiVersion}/api.json");
        app.UseSwaggerUi("foobar/api", "/foobar/v3/api.json");
    }
    ...
}

有什么区别吗?我想像一下,如果这种中间件的工作方式类似于python装饰器,或者只是执行某些功能并将结果传递给下一个功能的函数管道,那可能就很重要了.

Is there any difference? I imagine that if this middleware works similar to python decorators or just pipe of functions which do something and pass the results to the next function, then it might matter.

推荐答案

它与ASP.NET无关,但与OWIN托管实现无关.

It has nothing to do with ASP.NET but with OWIN hosting implementation.

订单很重要.例如,如果您注册一个应在其他任何中间件之后侦听错误的中间件,则有可能无法记录一些错误.

Order matters. For example, if you register a middleware that should listen errors after any other middleware, there's a chance of not being able to log some errors.

这只是一个假设的情况,但是它可以为您提供一个很好的提示,说明如果您更改中间件注册的顺序,其他情况可能会如何工作.

This is just an hypothetical case, but it can give you a good hint of how other scenarios may work if you change the order of middleware registrations.

或只是执行某些功能并将结果传递给的函数管道 下一个功能,那可能很重要.

or just pipe of functions which do something and pass the results to the next function, then it might matter.

就是这样!这就是为什么订单重要的原因.

That's it! This is the reason of why order matters.

这篇关于我可以按任何顺序将中间件应用到应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆