ASP.NET ServiceProvider
last modified April 3, 2025
In this article, we explore the ServiceProvider in ASP.NET 8. This core component manages dependency injection and service lifetime in applications.
ASP.NET's built-in dependency injection system uses ServiceProvider to resolve and manage services. It's essential for writing modular, testable applications.
Basic Definition
The ServiceProvider in ASP.NET is the container that manages registered services. It implements the IServiceProvider interface and handles service resolution.
Services are registered with specific lifetimes: transient, scoped, or singleton. The ServiceProvider creates and disposes services according to these lifetimes.
Transient services are created each time they're requested. Scoped services are created once per client request. Singleton services are created once and reused.
ASP.NET ServiceProvider Example
The following example demonstrates ServiceProvider usage with different service lifetimes and service resolution.
var builder = WebApplication.CreateBuilder(args); // Register services with different lifetimes builder.Services.AddTransient<ITransientService, TransientService>(); builder.Services.AddScoped<IScopedService, ScopedService>(); builder.Services.AddSingleton<ISingletonService, SingletonService>(); var app = builder.Build(); app.MapGet("/", (HttpContext context) => { var serviceProvider = context.RequestServices; var transient1 = serviceProvider.GetRequiredService<ITransientService>(); var transient2 = serviceProvider.GetRequiredService<ITransientService>(); var scoped1 = serviceProvider.GetRequiredService<IScopedService>(); var scoped2 = serviceProvider.GetRequiredService<IScopedService>(); var singleton1 = serviceProvider.GetRequiredService<ISingletonService>(); var singleton2 = serviceProvider.GetRequiredService<ISingletonService>(); return Results.Ok(new { TransientSame = transient1.Id == transient2.Id, ScopedSame = scoped1.Id == scoped2.Id, SingletonSame = singleton1.Id == singleton2.Id, Transient1 = transient1.Id, Transient2 = transient2.Id, Scoped1 = scoped1.Id, Scoped2 = scoped2.Id, Singleton1 = singleton1.Id, Singleton2 = singleton2.Id }); }); app.Run();
This sets up an ASP.NET application with three services registered with different lifetimes. The endpoint demonstrates how ServiceProvider resolves these services.
public interface ITransientService { Guid Id { get; } } public interface IScopedService { Guid Id { get; } } public interface ISingletonService { Guid Id { get; } } public class TransientService : ITransientService { public Guid Id { get; } = Guid.NewGuid(); } public class ScopedService : IScopedService { public Guid Id { get; } = Guid.NewGuid(); } public class SingletonService : ISingletonService { public Guid Id { get; } = Guid.NewGuid(); }
This defines the service interfaces and implementations. Each service has a Guid Id property to demonstrate instance creation behavior with different lifetimes.
When you call the endpoint, you'll see that transient services get new instances each resolution. Scoped services reuse the same instance per request. Singleton services maintain one instance for the application lifetime.
The GetRequiredService
method throws an exception if the service
isn't registered. For optional services, use GetService
which
returns null for unregistered services.
Source
Microsoft ASP.NET Dependency Injection Documentation
In this article, we have explored the ServiceProvider in ASP.NET 8. This powerful component is fundamental to ASP.NET's dependency injection system.
Author
List all ASP.NET tutorials.