liveBook · Manning

Func<T>

As explained in ASP.NET Core in Action, 3e

Func<Task> is a .NET delegate type representing a parameterless function that returns an asynchronous Task.

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.
CH31_F03_Lock3

Trace how invoking next() moves through middleware and how omitting it short-circuits the pipeline. section 31.1.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains continuation and short-circuiting with middleware examples, but does not detail delegate implementation internals.
Examples
Uses a /ping health check and response-header middleware to demonstrate continuation and callback timing.
What it assumes
Assumes familiarity with ASP.NET Core middleware, HttpContext, asynchronous methods, and request pipelines.
What you can do after
Apply it via ASP.NET Core Use middleware to continue or short-circuit request processing.
How it connects
Connects Func<Task> with ASP.NET Core middleware, the Use extension method, HttpContext, asynchronous Tasks, and lambda expressions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.