liveBook · Manning

Invoke method

As explained in Pro ASP.NET Core 7, Tenth Edition

An Invoke method is a middleware method convention that ASP.NET Core calls to process a request and may resolve its declared dependencies.

An Invoke method is a convention used by ASP.NET Core middleware and view components. Middleware can declare request-scoped dependencies on its parameters, while a view component uses Invoke to produce content. section 14.4.2 section 24.3

Use the method signature to control request or component behavior:

  • Declare a middleware dependency such as IResponseFormatter on Invoke rather than the constructor when it must be resolved for every request. section 14.4.2
  • Define Invoke or InvokeAsync on a view component to receive context data and return rendered or computed content. section 24.5.1 section 24.3
  • Provide every required view-component parameter; missing values prevent rendering without displaying an error, unless a default is defined. section 24.5.1

Resolve a transient service per request

A WeatherMiddleware constructor receives only RequestDelegate, while Invoke receives IResponseFormatter. ASP.NET Core resolves the formatter each time a request is processed, so a transient service can produce a new identifier for each response. section 14.4.2

Pass context into a view component

CitySummary.Invoke(string themeName = "success") stores the theme and returns city-count and population data. Razor can provide theme-name="danger" or omit it to use the default.

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains signatures, parameter binding, dependency lifetimes, and defaults, but does not detail framework internals beyond resolution timing.
Examples
Uses WeatherMiddleware, IResponseFormatter, CitySummary, theme-name attributes, and city-count and population summaries.
What it assumes
Assumes familiarity with ASP.NET Core middleware, dependency injection, Razor views, and view components.
What you can do after
Apply it via ASP.NET Core middleware and view components to control request-time services and generated response content.
How it connects
Connects middleware, dependency injection, transient services, view components, Razor views, and partial views.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.