Func<Task> represents a parameterless function that returns an asynchronous Task. In ASP.NET Core middleware, it points to the remaining request pipeline. section 31.1.3
Middleware uses the callback to control pipeline execution:
- Call next() to execute the rest of the pipeline. section 31.1.3
- Skip next() to short-circuit the request and produce a response immediately. section 31.1.3
- Run code before and after awaiting next(), while avoiding response changes after downstream streaming begins. section 31.1.3
Health-check middleware
For a /ping request, write "pong" without calling next(); otherwise, await next() so later middleware handles the request. section 31.1.3
Figure 31.3 Three pieces of middleware, created with the Use extension. Invoking the provided Func<Task> using next() invokes the rest of the pipeline. Each middleware component can run code before and after calling the rest of the pipeline, or it can choose to not call next() to short-circuit the pipeline.
Trace how invoking next() moves through middleware and how omitting it short-circuits the pipeline. section 31.1.3
