liveBook · Manning

GetRequiredService<T>()

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

IServiceProvider.GetRequiredService is a .NET dependency-injection extension method that resolves a required service and throws when no registration is available.

GetRequiredService<T>() is an IServiceProvider extension method that resolves a registered service by its generic type. Unlike GetService<T>(), it throws an exception when no registration is available, making missing dependencies fail immediately. section 14.3.2

Resolving a formatter from HttpContext

An endpoint can obtain IResponseFormatter through HttpContext.RequestServices and use it to format the response. With IResponseFormatter registered in Program.cs, requesting /endpoint/class produces the formatter's response. section 14.3.2

Listing 14.17 Using the HttpContext in the WeatherEndpoint.cs file in the Platform folder
using Platform.Services;

namespace Platform {
    public class WeatherEndpoint {
        
        public static async Task Endpoint(HttpContext context) {
            IResponseFormatter formatter = context.RequestServices
                .GetRequiredService<IResponseFormatter>();
            await formatter.Format(context,
                "Endpoint Class: It is cloudy in Milan");
        }
    }
}

The complete endpoint listing shows the service-resolution call in its request-processing context. section 14.3.2

Choose the resolution method according to how much service-selection behavior the consumer needs:

  • Use GetRequiredService<T>() when one service is required; with repeated registrations, resolution selects the most recently registered implementation. section 14.5.4
  • Use GetServices<T>() when the consumer must inspect all implementations and select one, such as the formatter whose RichOutput property is true. section 14.5.4
  • Prefer constructor injection for endpoint classes when possible; ActivatorUtilities can create the class while resolving constructor dependencies. section 14.3.2

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

How deep it goes
Defines generic resolution, missing-registration exceptions, and latest-registration behavior, then contrasts collection resolution and constructor injection.
Examples
Uses WeatherEndpoint, IResponseFormatter, endpoint/class, and multiple formatter implementations including GuidService.
What it assumes
Assumes familiarity with ASP.NET Core service registration and HttpContext while demonstrating service retrieval from zero.
What you can do after
Apply it via HttpContext.RequestServices to obtain a required service and diagnose absent registrations through the thrown exception.
How it connects
Connects IServiceProvider, dependency injection, GetService<T>(), GetServices<T>(), constructor injection, and ActivatorUtilities.

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.