IServiceCollection Interface
Microsoft.Extensions.DependencyInjectionRepresents a collection of service descriptors that can be used to define the services available for dependency injection.
Summary
The IServiceCollection
interface is central to configuring dependency injection in .NET applications. It provides a fluent API to register services and their corresponding implementation types or factory functions. Services registered with an IServiceCollection
can then be used to create a ServiceProvider
, which is responsible for resolving those services.
Key operations include adding transient, scoped, and singleton services, as well as configuring options and adding delegates for service creation.
Members
-
Add(ServiceDescriptor serviceDescriptor)
Adds the specified ServiceDescriptor to the collection.
-
Add(Type serviceType, Type implementationType, ServiceLifetime lifetime)
Adds a new service descriptor to the collection.
-
Add(Type serviceType, Func<IServiceProvider, object> implementationFactory, ServiceLifetime lifetime)
Adds a new service descriptor to the collection with a factory function.
-
AddTransient<TService>()
Adds a singleton service of the specified type
TService
.A transient service lifetime means that a new instance of the service will be created every time it is requested. -
AddTransient<TService, TImplementation>()
Adds a transient service of the specified type
TService
with a specified implementation typeTImplementation
.A transient service lifetime means that a new instance of the service will be created every time it is requested. -
AddScoped<TService>()
Adds a scoped service of the specified type
TService
.A scoped service lifetime means that a new instance of the service will be created once per client request. -
AddScoped<TService, TImplementation>()
Adds a scoped service of the specified type
TService
with a specified implementation typeTImplementation
.A scoped service lifetime means that a new instance of the service will be created once per client request. -
AddSingleton<TService>()
Adds a singleton service of the specified type
TService
.A singleton service lifetime means that a single instance of the service will be created and reused throughout the application. -
AddSingleton<TService, TImplementation>()
Adds a singleton service of the specified type
TService
with a specified implementation typeTImplementation
.A singleton service lifetime means that a single instance of the service will be created and reused throughout the application. -
Replace(ServiceDescriptor descriptor)
Replaces the first descriptor matching
descriptor
withdescriptor
. -
Configure<TOptions>(Action<TOptions> configureOptions) where TOptions : class
Configures a named options checkpoint.
See Also
Example Usage:
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
public interface IMyService
{
void DoSomething();
}
public class MyService : IMyService
{
public void DoSomething()
{
Console.WriteLine("Doing something...");
}
}
public class Program
{
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
using (var scope = host.Services.CreateScope())
{
var myService = scope.ServiceProvider.GetRequiredService<IMyService>();
myService.DoSomething();
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((context, services) =>
{
// Add a transient service
services.AddTransient<IMyService, MyService>();
// Add a singleton service with a factory
services.AddSingleton<ISingletonService>(provider => new SingletonService(provider.GetRequiredService<IOptions<MySettings>>().Value));
// Configure options
services.Configure<MySettings>(context.Configuration.GetSection("MySettings"));
});
}
public class MySettings
{
public string SomeSetting { get; set; }
}
public interface ISingletonService { }
public class SingletonService : ISingletonService
{
public SingletonService(MySettings settings) { }
}