Can I apply middleware to the application in any order?

In C # ASP.NET does middleware application matter?

The following two code snippets:

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(); } ... } 

and this one

 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"); } ... } 

Is there any difference? I guess if this middleware works like python decorators or just a pipe of functions that do something and pass the results to the next function, then that might make a difference.

+2
source share
1 answer

This has nothing to do with ASP.NET, but with an OWIN hosting implementation.

The order of questions. For example, if you register middleware that should listen for errors after any other middleware, there is a possibility that you will not be able to register some errors.

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

or just a pipe of functions that do something and pass the results to the next function, then it can make a difference.

What is it! That is why order is the reason.

+2
source

Source: https://habr.com/ru/post/1265442/


All Articles