CodeBlanks Logo Icon CodeBlanks

ServiceCollection

C# course · Advanced · Lesson 57/83

Learn Microsoft.Extensions.DependencyInjection ServiceCollection.

ServiceCollection

Microsoft.Extensions.DependencyInjection.ServiceCollection is .NET's official DI container. You register services with AddTransient, AddScoped or AddSingleton; you produce a provider with BuildServiceProvider().

From the provider you retrieve a registered service with provider.GetRequiredService<T>(). The container resolves dependencies automatically: if a service requires another dependency, the container instantiates and passes it too. This is a major convenience in mid-sized and large applications.

IServiceCollection

IServiceCollection is the interface; ServiceCollection is its implementation. Many .NET libraries take IServiceCollection for registration, so you depend on the interface rather than the concrete class.

Registration usually maps an interface to an implementation: services.AddTransient<ILogger, ConsoleLogger>(). This makes requests for ILogger return a ConsoleLogger instance. To change the implementation you only change the registration; consumers stay the same.

Exercise Register and resolve a service

In Main, create the container with var services = new ServiceCollection();. Register with services.AddTransient<ILogger, ConsoleLogger>();. Build the provider with var provider = services.BuildServiceProvider();. Get the logger with var logger = provider.GetRequiredService<ILogger>(); and print with logger.Log("Hello");.

Show example solution
var services = new ServiceCollection(); services.AddTransient<ILogger, ConsoleLogger>(); var provider = services.BuildServiceProvider(); var logger = provider.GetRequiredService<ILogger>(); logger.Log("Hello");
Example solution:
using System;
using Microsoft.Extensions.DependencyInjection;

interface ILogger { void Log(string msg); }

class ConsoleLogger : ILogger
{
    public void Log(string msg) => Console.WriteLine(msg);
}

class Program
{
    static void Main()
    {
        var services = new ServiceCollection();
        services.AddTransient<ILogger, ConsoleLogger>();
        var provider = services.BuildServiceProvider();
        var logger = provider.GetRequiredService<ILogger>();
        logger.Log("Hello");
    }
}

Test yourself

  1. Match the method to its behavior

    AddTransient
    New each time
    AddScoped
    Per scope
    AddSingleton
    Single instance
    BuildServiceProvider
    Builds provider
  2. Complete the AddTransient registration

    services.____<ILogger, ConsoleLogger>();
    Show answer
    services.AddTransient<ILogger, ConsoleLogger>();
  3. How are services registered in ServiceCollection?

    • With AddTransient/AddScoped/AddSingleton
    • By calling Add directly
    • Only with the new keyword
    • With the Configure method
    Show answer

    With AddTransient/AddScoped/AddSingleton — Correct. Services are registered with AddTransient/AddScoped/AddSingleton.

All lessons in Advanced