EtherGizmos.Extensions.DependencyInjection.ChildContainers 0.1.0-ci.2

This is a prerelease version of EtherGizmos.Extensions.DependencyInjection.ChildContainers.
dotnet add package EtherGizmos.Extensions.DependencyInjection.ChildContainers --version 0.1.0-ci.2
NuGet\Install-Package EtherGizmos.Extensions.DependencyInjection.ChildContainers -Version 0.1.0-ci.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EtherGizmos.Extensions.DependencyInjection.ChildContainers" Version="0.1.0-ci.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EtherGizmos.Extensions.DependencyInjection.ChildContainers --version 0.1.0-ci.2
#r "nuget: EtherGizmos.Extensions.DependencyInjection.ChildContainers, 0.1.0-ci.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EtherGizmos.Extensions.DependencyInjection.ChildContainers as a Cake Addin
#addin nuget:?package=EtherGizmos.Extensions.DependencyInjection.ChildContainers&version=0.1.0-ci.2&prerelease

// Install EtherGizmos.Extensions.DependencyInjection.ChildContainers as a Cake Tool
#tool nuget:?package=EtherGizmos.Extensions.DependencyInjection.ChildContainers&version=0.1.0-ci.2&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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 107 3/7/2024
0.1.0-ci.1 54 3/3/2024