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

