EtherGizmos.Extensions.DependencyInjection.ChildContainers
0.1.0-ci.1
See the version list below for details.
dotnet add package EtherGizmos.Extensions.DependencyInjection.ChildContainers --version 0.1.0-ci.1
NuGet\Install-Package EtherGizmos.Extensions.DependencyInjection.ChildContainers -Version 0.1.0-ci.1
<PackageReference Include="EtherGizmos.Extensions.DependencyInjection.ChildContainers" Version="0.1.0-ci.1" />
paket add EtherGizmos.Extensions.DependencyInjection.ChildContainers --version 0.1.0-ci.1
#r "nuget: EtherGizmos.Extensions.DependencyInjection.ChildContainers, 0.1.0-ci.1"
// Install EtherGizmos.Extensions.DependencyInjection.ChildContainers as a Cake Addin
#addin nuget:?package=EtherGizmos.Extensions.DependencyInjection.ChildContainers&version=0.1.0-ci.1&prerelease
// Install EtherGizmos.Extensions.DependencyInjection.ChildContainers as a Cake Tool
#tool nuget:?package=EtherGizmos.Extensions.DependencyInjection.ChildContainers&version=0.1.0-ci.1&prerelease
Child Containers for .NET Dependency Injection
This package offers a solution for managing dependencies in .NET applications utilizing the standard Microsoft-provided Dependency Injection. Introducing the concept of child containers, it enables resolving services within the parent container after other parent services have been configured. This seamlessly allows for the utilization of singleton or transient dependencies such as IOptions, even when the target service lacks direct access to an IServiceProvider in its configuration methods.
With this package, you can efficiently manage dependencies without the need to construct services before the service container is built in order to configure other services. This approach fosters cleaner and more organized dependency management in your .NET applications.
Example Usage
For applications utilizing frameworks like MassTransit, which might not directly support pulling services from an IServiceProvider during configuration, this package offers seamless integration with services like IOptions. By adding MassTransit to a child container, which maintains its own set of dependencies resolved at runtime, you can ensure that services such as IOptions are readily available when configuring MassTransit within the child container, without requiring direct access to an IServiceProvider from MassTransit.
This approach extends to other packages that may not inherently support pulling services from an IServiceProvider during configuration.
builder.Services
.AddOptions<UsageOptions>()
.Configure<IConfiguration>((opt, conf) =>
{
var path = "Connections:Use";
conf.GetSection(path)
.Bind(opt);
opt.AssertValid(path);
});
builder.Services
.AddChildContainer((childServices, parentServices) =>
{
var usageOptions = parentServices
.GetRequiredService<IOptions<UsageOptions>>()
.Value;
childServices.AddMassTransit(opt =>
{
// Configuration based on UsageOptions
if (usageOptions.MessageBroker == MessageBrokerType.InMemory)
{
opt.UsingInMemory((context, conf) =>
{
// In-memory configuration
});
}
else if (usageOptions.MessageBroker == MessageBrokerType.RabbitMQ)
{
var rabbitMQOptions = parentServices
.GetRequiredService<IOptions<RabbitMQOptions>>()
.Value;
opt.UsingRabbitMq((context, conf) =>
{
// RabbitMQ configuration based on RabbitMQOptions
// ...
});
}
else
{
throw new InvalidOperationException($"Unknown message broker type: {usageOptions.MessageBroker}");
}
});
})
.ImportLogging() // Import logging from parent container
.ForwardScoped<ISendEndpointProvider>(); // Forward ISendEndpointProvider to parent container
After building the service provider, you can resolve the ISendEndpointProvider
by injecting ISendEndpointProvider
into any service within the application or by directly calling services.GetRequiredService<ISendEndpointProvider>()
from the parent provider.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.DependencyInjection (>= 6.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.4)
-
net6.0
- Microsoft.Extensions.DependencyInjection (>= 6.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.4)
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 6.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.1.0-ci.2 | 123 | 3/7/2024 |
0.1.0-ci.1 | 55 | 3/3/2024 |