rebus-org / Rebus.ServiceProvider

:bus: Microsoft Extensions Dependency Injection container adapter for Rebus
https://mookid.dk/category/rebus
Other
67 stars 34 forks source link

Accessing IConfiguration when using AddRebusService with a generic IHostBuilder #69

Closed SamPersson closed 2 years ago

SamPersson commented 2 years ago

When trying to use AddRebusService() to start multiple Rebus instances in a console application using the generic IHostBuilder (so not using WebApplicationBuilder) I could not find any easy way to access the app configuration when registering the instance specific services.

var host = Host.CreateDefaultBuilder()
    .AddRebusService(services =>
    {
        // Where can "configuration" come from here?
        services.Configure<AuthenticationOptions>(configuration.GetSection("Authentication"));

        ...
    })
    .Build();

The common methods on IHostBuilder like ConfigureServices give you a HostBuilderContext that contains the Configuration and some other useful things, in addition to the IServiceCollection. Is there another way to access the app configuration that I did not think of? Otherwise maybe adding overloads for AddRebusService() that forwards the HostBuilderContext to the configureServices callback like this could be useful for console applications? https://github.com/rebus-org/Rebus.ServiceProvider/compare/master...SamPersson:Rebus.ServiceProvider:host-builder-context That would enable:

var host = Host.CreateDefaultBuilder()
    .AddRebusService((hostContext, services) =>
    {
        services.Configure<AuthenticationOptions>(hostContext.Configuration.GetSection("Authentication"));

        ...
    })
    .Build();
mookid8000 commented 2 years ago

Another oversight!

Otherwise maybe adding overloads for AddRebusService() that forwards the HostBuilderContext to the configureServices callback like this could be useful for console applications? https://github.com/rebus-org/Rebus.ServiceProvider/compare/master...SamPersson:Rebus.ServiceProvider:host-builder-context

This sounds like the way to go! Are you kneading another PR? 😉

SamPersson commented 2 years ago

Sure :)

mookid8000 commented 2 years ago

Fixed by @SamPersson 's PR in Rebus.ServiceProvider 8.2.0