liveBook · Manning

Services property

As explained in ASP.NET Core in Action, 3e

The Services property is the WebApplicationBuilder property through which application and framework services are registered for dependency injection.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains IServiceCollection, extension-method registration, and startup failure, while deferring custom-service mechanics to dependency-injection coverage.
Examples
Uses AddRazorPages(), AddHttpLogging(), and a custom TaxCalculator to demonstrate framework, optional, and application-service registration.
What it assumes
Assumes a basic ASP.NET Core application and teaches that dependency-injected services must be registered before use.
What you can do after
Configure services through WebApplicationBuilder.Services and diagnose startup failures caused by missing framework registrations.
How it connects
Connects dependency injection, the IoC container, IServiceCollection, WebApplicationBuilder, Razor Pages, and middleware.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.