IServiceCollection is the application’s list of known service registrations. WebApplicationBuilder exposes it through builder.Services, allowing the dependency-injection container to provide services when application classes declare dependencies. section 3.7.1 section 8.4
Use the collection to:
- Register framework features such as Razor Pages and authentication through extension methods. section 8.4
- Register application services, such as a tax calculator, so the container can supply them to dependent classes. section 3.7.1
- Finish registration before calling Build(); afterward, the application’s service definitions cannot be changed. section 3.7.1
Registering Razor Pages
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); WebApplication app = builder.Build(); app.MapRazorPages(); app.Run(); AddRazorPages() registers the services required by Razor Pages, while MapRazorPages() registers their endpoints. Omitting the service registration causes the application to fail when it starts. section 8.4
builder.Services.AddHttpLogging(opts => opts.LoggingFields = HttpLoggingFields.RequestProperties);
Inspect a compact example of configuring HTTP logging services through an IServiceCollection extension method. section 3.7.1

