Closed Tratcher closed 6 years ago
There is no definitive order, only some relative dependencies between individual components. E.g. StatusCodes should come before components that generate responses without bodies. StaticFiles are often early in the pipeline for efficiency, there can be a lot of them. Auth should come before components that require auth, like MVC. Caching should come before components that generate cacheable responses like MVC. WebSockets should come before components that use them like SignalR. Compression should come before components that generate compressable responses, and after caching if you want to cache the compressed version.
Maybe what we need to explain is if a middleware handles requests as they arrive, or if it inserts itself to react to responses as they're generated. E.g. caching and compression attach to a request as it comes in and monitor the response to see if they need to react. StaticFiles by contract handles any requests it can immediately.
From @guylando on January 11, 2018 23:15
Thanks, Maybe a dependency tree\graph can be built\maintained in the documentation instead of one startup.cs because the possibilities are better described in a tree\graph than in a linear code? Such tree\graph would probably be very useful for many
Even a table might help as a quick reference:
Middleware | Ordering |
---|---|
UseStatusCodePagesWithRedirects | Before components that set status codes |
UseAuthentication | Before HttpContext.User is needed. Terminal for OAuth callbacks. |
UseResponseCaching | Before components that want caching |
UseWebSockets | Before components that want to use WebSockets |
UseResponseCompression | Before components that want to use compression |
UseStaticFiles | Terminal if a request matches files |
UseSignalR | Terminal for matching routes / hubs |
UseMvc | Terminal if a request matches routes / controllers & actions |
Possibly sorted by pass through vs terminal. That sorting alone would give you a sense of the ordering.
From @guylando on January 12, 2018 16:45
That's great! thanks
@Tratcher How about I add the table to this doc? https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware?tabs=aspnetcore2x
Yes that looks like the right doc. Try adding a column to the existing table in built-in middleware, with a reference to the ordering section.
Imo there should be just analyzer/refactoring that checks the middleware order for you and offers re-ordering. Or a fody weaver that re-orders IL instructions... It's pretty annoying to look for the docs everytime when adding a middleware
From @guylando on January 11, 2018 12:19
I can't find an example which shows what is the correct order for all the following middleware when used together. It would be nice if there would be a maintained startup.cs containing ALL possible middleware.
UseStatusCodePagesWithRedirects UseStaticFiles UseAuthentication UseResponseCaching UseWebSockets UseSignalR UseResponseCompression UseMvc
Would save a lot of time and errors having it. Thanks!
Copied from original issue: aspnet/Home#2769