David's Blog

Understanding Microsoft.Extensions.DependencyInjection A Deep Dive

By David Li on 2024-11-26T17:49:54.000Z

Understanding Microsoft.Extensions.DependencyInjection: A Deep Dive

Microsoft.Extensions.DependencyInjection is a lightweight, extensible dependency injection (DI) library designed for use with .NET applications. This library provides a minimalistic yet powerful framework for managing dependencies in .NET applications. In this article, we will explore the key features of this library, discuss its benefits, and walk through some examples to demonstrate how to use it effectively in your projects.

What is Dependency Injection?

Dependency Injection is a design pattern that promotes the separation of concerns in software development. It encourages loosely coupled components by making dependencies external to the components that use them. DI enables better maintainability, testability, and flexibility of software applications.

Why Microsoft.Extensions.DependencyInjection?

There are several DI libraries available for .NET developers, such as Autofac, Ninject, and Unity. However, Microsoft.Extensions.DependencyInjection is the default dependency injection framework provided by Microsoft for .NET Core and .NET 5+ applications. It is lightweight, fast, and extensible, making it a suitable choice for small to large projects.

Getting Started

To start using Microsoft.Extensions.DependencyInjection in your project, you need to install the Microsoft.Extensions.DependencyInjection NuGet package. You can do this using the following command:

dotnet add package Microsoft.Extensions.DependencyInjection

Key Components

There are three main components in the Microsoft.Extensions.DependencyInjection library:

  1. IServiceCollection: This is the central component that holds the collection of service descriptors. It defines the services and their lifetimes.

  2. ServiceDescriptor: This is a representation of a service, including its type, implementation, and lifetime.

  3. IServiceProvider: This is the component responsible for creating instances of services, managing their lifetimes, and providing access to them.

Service Lifetimes

Microsoft.Extensions.DependencyInjection supports three service lifetimes:

  1. Transient: A new instance of the service is created each time it is requested. This is suitable for lightweight, stateless services.

  2. Scoped: An instance of the service is created per scope. In a web application, this means one instance per request.

  3. Singleton: A single instance of the service is created, and the same instance is used for all requests.

Registering Services

To register services with the DI container, you need to use the IServiceCollection interface. The following methods are commonly used for registering services:

  • AddTransient<TService, TImplementation>(): Registers a service with a transient lifetime.
  • AddScoped<TService, TImplementation>(): Registers a service with a scoped lifetime.
  • AddSingleton<TService, TImplementation>(): Registers a service with a singleton lifetime.


Let’s create a simple console application to demonstrate how to use Microsoft.Extensions.DependencyInjection.

using System;
using Microsoft.Extensions.DependencyInjection;

public interface IGreeter
    void Greet(string name);

public class ConsoleGreeter : IGreeter
    public void Greet(string name)
        Console.WriteLine($"Hello, {name}!");

public class Program
    static void Main(string[] args)
        // Configure the service collection
        var services = new ServiceCollection();
        services.AddSingleton<IGreeter, ConsoleGreeter>();

        // Build the service provider
        var serviceProvider = services.BuildServiceProvider();

        // Resolve and use the service
        var greeter = serviceProvider.GetService<IGreeter>();

In this example, we register the ConsoleGreeter service as a singleton with the IGreeter interface. We then build the service provider and use it to resolve an instance of IGreeter. Finally, we call the Greet method to display a greeting message.

Extending Microsoft.Extensions.DependencyInjection

One of the key features of this library is its extensibility. You can easily extend the library by implementing custom service providers or using third-party extensions. This allows you to add additional features or integrate with other DI libraries if needed.


Microsoft.Extensions.DependencyInjection is a powerful, lightweight dependency injection framework for .NET applications. It provides a simple yet extensible way to manage dependencies in your projects. By using this library, you can promote clean architecture, improve testability, and maintainability in your applications.

© Copyright 2024 by FriendlyUsers Tech Blog. Built with ♥ by FriendlyUser. Last updated on 2024-02-20.