The Services property on WebApplicationBuilder is the IServiceCollection used to register framework and application services for dependency injection. Register a service before code declares it as a dependency, then call Build() to create the application. section 3.7.1 section 3.8
Use the property to:
- Enable framework features with extension methods such as AddRazorPages(). section 8.4
- Register custom application services, such as a TaxCalculator, so the IoC container can provide them to dependent classes. section 3.7.1
- Configure optional framework services, such as HTTP logging, through methods such as AddHttpLogging(). section 3.7.1
Register Razor Pages services
WebApplicationBuilder builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorPages(); WebApplication app = builder.Build(); app.MapRazorPages(); app.Run(); AddRazorPages() registers the services required by Razor Pages; MapRazorPages() then exposes the pages as endpoints. Omitting the service registration causes an exception when the application starts. section 8.4
builder.Services.AddHttpLogging(opts => opts.LoggingFields = HttpLoggingFields.RequestProperties);
Configure HTTP logging services and select which request fields the middleware records. section 3.7.1
