liveBook · Manning

Service

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

A service is a class that provides a discrete piece of functionality to an application and can be registered with its dependency-injection container.

A service is a class that provides a piece of functionality to an ASP.NET Core application. It might come from a library or be code you write yourself. Keeping each service focused makes it easier to combine small pieces into a larger feature. section 3.7.1

Combining services

An order-total calculator might use a tax calculator and a shipping-cost service. Creating those dependencies with `new` inside the calculator ties it to particular implementations. Dependency injection lets the calculator declare what it needs and lets a container supply those objects. section 3.7.1 section 8.6

Register services in `Program.cs` through `WebApplicationBuilder.Services`, an `IServiceCollection`. Many core services are registered automatically; optional features need explicit registration. For example, `AddRazorPages()` adds the services needed by Razor Pages, while `MapRazorPages()` makes the pages available as endpoints. Missing a required registration can cause an exception when the application starts or requests a service. section 8.4 section 8.6

Listing 8.6 Registering the Razor Pages services with the DI container
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();    #1
 
WebApplication app = builder.Build();
app.MapRazorPages();                 #2
app.Run();
#1 The AddRazorPages extension method adds all necessary services to the IServiceCollection.
#2 Registers all the Razor Pages in your application as endpoints

Compare the service-registration call before `Build()` with the endpoint-mapping call afterward; they do different jobs. section 8.4

Code can request a registered service as an explicit dependency. A minimal API endpoint, for instance, can include `LinkGenerator` as a handler parameter; ASP.NET Core supplies its instance from the DI container. Applications can register their own services as well as framework services. section 7.6.2 section 9.0

Choose a lifetime when registering a service; it determines when the container creates a new instance:

  • Transient: a new instance for every request for the service. section 9.4
  • Scoped: one instance within a scope; ASP.NET Core gives each web request its own scope. section 9.4
  • Singleton: the same instance regardless of scope. section 9.4

Keep request-handling code focused on handling requests. A Razor Page handler can delegate business logic to a service, helping separate concerns and making the application easier to test. When choosing lifetimes, watch for a short-lived dependency captured by a longer-lived service; that combination can cause hard-to-debug problems. section 13.5 section 9.4

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

How deep it goes
Defines services, demonstrates registration and endpoint injection, and explains instance reuse through three lifetimes.
Examples
Uses e-commerce calculators and Razor Pages registration to illustrate service composition and setup.
What it assumes
Begins with the meaning of a service and a small e-commerce example before introducing container configuration.
What you can do after
Operate/configure it through `WebApplicationBuilder.Services`, request it in handlers, and choose a lifetime for its instances.
How it connects
Connects dependency injection, endpoint handlers, and service lifetimes.

Also explained in

Compare how 7 books explain service →

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.