Microsoft.Extensions.DependencyInjection

Provides types for configuring and managing dependency injection containers.

Classes

ServiceCollection

A collection of services to be added to an IServiceCollection.

public class ServiceCollection : IServiceCollection, ICollection, IEnumerable, IEnumerable

ServiceDescriptor

Describes a service registration.

public readonly struct ServiceDescriptor

ServiceProvider

A IServiceProvider that provides access to services.

public class ServiceProvider : IServiceProvider, IDisposable, ISupportRequiredService
Interfaces

IServiceCollection

Represents a collection of service descriptors.

public interface IServiceCollection : ICollection, IEnumerable, IEnumerable

IServiceProvider

Represents a container that can be used to resolve services.

public interface IServiceProvider

IServiceScope

Represents a scope for resolving services.

public interface IServiceScope : IDisposable

IServiceScopeFactory

Creates an IServiceScope which contains an IServiceProvider.

public interface IServiceScopeFactory
Enums

ServiceLifetime

Specifies the lifetime of a service.

public enum ServiceLifetime

Members:

  • 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