The project helps simplify working with dependency injection in ASP.NET Core.
You can install Calzolari.DependencyInjection.Extensions
from NuGet. You can install it from the NuGet Package Manager UI in Visual Studio or by running one of the following commands.
dotnet add package Calzolari.DependencyInjection.Extensions
Install-Package Calzolari.DependencyInjection.Extensions
Use the service extensions in your Startup.cs
's ConfigureServices
method, as you do with your other services.
The supported extensions are below.
Use this to unregister services from its concrete implementation or its interface.
public void ConfigureServices(IServiceCollection services)
{
services.Unregister<MyService>();
services.Unregister<IMyService>();
}
Use Replace
to replace a service with a new implementation for a specified scope.
public void ConfigureServices(IServiceCollection services)
{
services.Replace<IMyService, MyService>(ServiceLifetime.Scoped);
}
Use ReplaceTransient
to replace a service with a new implementation with a transient scope.
public void ConfigureServices(IServiceCollection services)
{
services.ReplaceTransient<IMyService, MyService>();
}
Use ReplaceScoped
to replace a service with a new implementation with a scope of Scoped
.
public void ConfigureServices(IServiceCollection services)
{
services.ReplaceScoped<IMyService, MyService>();
}
Use ReplaceSingleton
to replace a service with a new implementation with a scope of Singleton
.
public void ConfigureServices(IServiceCollection services)
{
services.ReplaceSingleton<IMyService, MyService>();
}
Use RegisterOptions
to either register options as a MyOptions
singleton (the appsettings.json
section name must match the MyOptions
name), or register options as a MyOptions
singleton from a given appsettings.json
section name.
public void ConfigureServices(IServiceCollection services)
{
services.RegisterOptions<MyOptions>(Configuration);
services.RegisterOptions<MyOptions>(Configuration, "SectionName");
}