1

I'm building .NetCore console app which uses SimpleRabbit (RabbitMQ). I am attempting to use HostBuilder() to this purpose (dependecy injection, configuration, services). I create a SimpleInjector container in the Bootstrap class. I would like to inject this container for use into HostBuilder.

As you can see below, the FooSubscriberService constructor is expecting a container. I see HostBuilder exposes a ConfigureContainer() method which sounds like what I'm looking for. I've looked around but I'm unclear as how to use this/inject the container.

Supporting Class Snippets

public class FooSubscriberService : IFooSubscriberService
{
    Container container;
    private readonly ILogger<FooSubscriberService> logger;

    public FooSubscriberService(Container container, ILogger<DiscoverySubscriberService> logger)
    {
        this.container = container;
        this.logger = logger;
    }
    
    ....
}

public class QueueManagementService : BasicRabbitService, IQueueManagementService
{
    ILogger _logger;
    public QueueManagementService(RabbitConfiguration config, ILogger<QueueManagementService> _logger) : base(config)
    {
        this._logger = _logger;
    }
    
    ....
}

Container Creation

public class Bootstrap
{
    public static Container container;

    public static void ConfigureServices(IConfigurationRoot configurationRoot)
    {
        container = new SimpleInjector.Container();
        container.Options.ResolveUnregisteredConcreteTypes = false;
        container.Options.EnableAutoVerification = false;

        RabbitConfiguration rabbitConfiguration = new RabbitConfiguration();
        configurationRoot.GetSection("RabbitConfiguration").Bind(rabbitConfiguration);

        var runtimeModel = new RuntimeConfigurationModel();
        var runtimeStatus = new RuntimeServicesStatus();
        container.ConfigureServices(rabbitConfiguration, runtimeModel, runtimeStatus, searchJobStatus);
    }
}

Code Snippet for initializing and running HostBuilder.

Bootstrap.ConfigureServices(configurationRoot);
var container = Bootstrap.container;

var builder = new HostBuilder()
    .ConfigureAppConfiguration((hostingContext, config) =>
    {
        config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    })
    .ConfigureServices((context, services) =>
    {
        var config = context.Configuration;
        services
            .AddRabbitConfiguration(configurationRoot.GetSection("RabbitConfiguration"))
            .AddSubscriberConfiguration(configurationRoot.GetSection("Subscribers"))
            .AddPublisherServices()
            .AddSubscriberServices()
            .AddSingletonMessageHandler<FooSubscriberService>()
            .AddSingleton<IQueueManagementService, QueueManagementService>()
            .AddSingleton<IHostedService, FooConsoleService>();
    });
    
//  ??? .ConfigureContainer<Container>( => { }); ???
    
await builder.RunConsoleAsync();

What is the method for injecting my Container into HostBuilder()? Are there additional NuGet packages I need to use?

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.