liveBook · Manning

Service

As explained in Building Web APIs with ASP.NET Core

An ASP.NET Core service is a registered application component that provides functionality to other components through dependency injection.

An ASP.NET Core service is a reusable application component that provides functionality the application depends on. Services support modularity, testability, and maintainability, and communicate with other components through interfaces. section 1.2.1 section 1.3

ASP.NET Core handles a service through three related steps:

  • Implement the service behind a dedicated interface or base class. The framework, application, or a third-party package can provide the abstraction and implementation. section 1.2.1
  • Register and configure the service in the built-in IServiceProvider service container, normally through the application's Program.cs entry point. section 1.2.1 section 1.2.2
  • Inject the registered service where another component requires it, allowing dependency injection to provide the implementation. section 1.2.1

Registering application services

Program.cs creates the web application, adds required services through builder.Services, builds the application, and then configures middleware. ASP.NET Core naming conventions commonly use Add-prefixed extension methods for service registration, as shown by AddControllers, AddEndpointsApiExplorer, and AddSwaggerGen. section 1.2.2

var builder = WebApplication.CreateBuilder(args);    #1
 
// Add services to the container.
 
builder.Services.AddControllers();                   #2
// Learn more about configuring Swagger/OpenAPI
// at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();          #2
builder.Services.AddSwaggerGen();                    #2
 
var app = builder.Build();                           #3
 
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();                                #4
    app.UseSwaggerUI();                              #4
}
 
app.UseHttpsRedirection();                           #4
 
app.UseAuthorization();                              #4
 
app.MapControllers();                                #4
 
app.Run();                                           #5

Inspect the Program.cs listing to follow service registration beside application construction, middleware configuration, controller mapping, and application startup. section 1.2.2

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains interfaces, registration, configuration, injection, and Program.cs conventions, but defers detailed service lifetimes and implementation code.
Examples
Demonstrates AddControllers, AddEndpointsApiExplorer, and AddSwaggerGen in a Program.cs application startup listing.
What it assumes
Assumes basic ASP.NET Core application structure while introducing dependency injection, service containers, interfaces, and application dependencies.
What you can do after
Operate/configure services in Program.cs using builder.Services and Add-prefixed extension methods.
How it connects
Connects services with dependency injection, inversion of control, service containers, interfaces, Program.cs, and middleware.

Also explained in

Compare how 2 books explain service →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.