Microsoft.Extensions.DependencyInjection
Provides types for configuring and managing dependency injection containers.
Classes
ServiceCollection
A collection of services to be added to an IServiceCollection
.
ServiceDescriptor
Describes a service registration.
public readonly struct ServiceDescriptorServiceProvider
A IServiceProvider
that provides access to services.
Interfaces
IServiceCollection
Represents a collection of service descriptors.
public interface IServiceCollection : ICollectionIServiceProvider
Represents a container that can be used to resolve services.
public interface IServiceProviderIServiceScope
Represents a scope for resolving services.
public interface IServiceScope : IDisposableIServiceScopeFactory
Creates an IServiceScope
which contains an IServiceProvider
.
Enums
ServiceLifetime
Specifies the lifetime of a service.
public enum ServiceLifetimeMembers:
Singleton
Scoped
Transient
Usage Examples
Here's a basic example of how to configure and use the dependency injection system:
using Microsoft.Extensions.DependencyInjection;
using System;
// Define interfaces and implementations
public interface IMyService {}
public class MyService : IMyService {}
public interface IAnotherService
{
void DoSomething();
}
public class AnotherService : IAnotherService
{
private readonly IMyService _myService;
public AnotherService(IMyService myService)
{
_myService = myService ?? throw new ArgumentNullException(nameof(myService));
}
public void DoSomething()
{
Console.WriteLine("AnotherService doing something with IMyService.");
}
}
public class Program
{
public static void Main(string[] args)
{
// 1. Create a service collection
var services = new ServiceCollection();
// 2. Register services
// Singleton: Single instance throughout the application's lifetime.
services.AddSingleton();
// Scoped: Single instance per request (e.g., per HTTP request in a web app).
// For console apps, it's typically per service provider instance.
services.AddScoped();
// Transient: New instance every time it's requested.
// services.AddTransient();
// 3. Build the service provider
ServiceProvider serviceProvider = services.BuildServiceProvider();
// 4. Resolve and use services
using (var scope = serviceProvider.CreateScope()) // Create a scope for Scoped services
{
var myService1 = scope.ServiceProvider.GetService();
var myService2 = scope.ServiceProvider.GetService();
var anotherService1 = scope.ServiceProvider.GetService();
Console.WriteLine($"myService1 == myService2: {object.ReferenceEquals(myService1, myService2)}"); // Should be true for Singleton
anotherService1.DoSomething();
}
// Another scope to demonstrate Scoped lifetime
using (var scope = serviceProvider.CreateScope())
{
var myService3 = scope.ServiceProvider.GetService();
var anotherService2 = scope.ServiceProvider.GetService();
Console.WriteLine($"myService1 == myService3: {object.ReferenceEquals(myService1, myService3)}"); // Should be true for Singleton
Console.WriteLine($"anotherService1 == anotherService2: {object.ReferenceEquals(anotherService1, anotherService2)}"); // Should be false for Scoped across scopes
anotherService2.DoSomething();
}
// Clean up the service provider
serviceProvider.Dispose();
}
}
Key Concepts
- Service Lifetime: Determines how long an instance of a service is kept alive. The common lifetimes are:
Singleton
: A single instance is created and reused throughout the application.Scoped
: A single instance is created per scope. In web applications, this typically corresponds to the lifetime of an HTTP request.Transient
: A new instance is created every time the service is requested.
IServiceCollection
: Used to register services and their descriptors. This is typically built up at the start of an application.IServiceProvider
: Used to resolve registered services. An instance ofIServiceProvider
is usually created from anIServiceCollection
.ServiceDescriptor
: Describes how a service is registered, including its type, implementation type, and lifetime.