Extensions for .NET Core Dependency Injection container, that allow to resolve dependencies by key.
Current implementation of .NET Core Dependency Injection container does not support registration and resolving dependencies by name or key.
Can be installed via Nuget
Install-Package DependencyInjection.Extensions.NamedDependencies
or .NET CLI
dotnet add package DependencyInjection.Extensions.NamedDependencies
Register named services with IServiceCollection
services.AddScoped<IEventBus, AzureServiceBusPersistance, string>("azureServiceBus");
services.AddScoped<IEventBus, RabbitMQServicePersistance, string>("rabbitMQ");
Inject IServiceProvider
interface where you need to resolve dependency and call GetService
method with key provided
var eventBus = serviceProvider.GetService<IEventBus, string>(eventBusKey);
Not only string
can be used as type for key parameter. All extensions is parameterized, so Enum
, Guid
or another types can be used as key.
This extensions are built with .NET Standart 2.0 and depends on Microsoft.Extensions.DependencyInjection
DependencyInjection.Extensions.NamedDependencies
follows the versioning of the Microsoft.Extensions.DependencyInjection, since it's main dependency.
If you have any questions or proposals - please create issue or PR.