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.
