rebus-org / Rebus.ServiceProvider

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

It should be possible to access IServiceProvider from onCreated #94

Closed srogovtsev closed 3 weeks ago

srogovtsev commented 4 weeks ago

Documentation shows the following example:

services.AddRebus(
    configure => configure
        .Transport(...),

    onCreated: async bus => {
        await Task.WhenAll(
            bus.Subscribe<SomeEvent>(),
            bus.Subscribe<AnotherEvent>(),
            bus.Subscribe<LastEvent>()
        );
    }
);

This is nice, but in our case information required for the subscriptions is stored somewhere else in the DI container. I think that onCreated should accept Func<IServiceProvider,IBus,Task> instead of current Func<IBus,Task>.

From the brief glimpse at the code I can see that we definitely have access to the IServiceProvider at the moment of onCreate call, so this should be pretty straightforward.

https://github.com/rebus-org/Rebus.ServiceProvider/blob/2429f8cec05ff89d108837fda9db2da830073a11/Rebus.ServiceProvider/ServiceProvider/Internals/RebusInitializer.cs#L117-L121

I'll try to draft a PR later this week.

mookid8000 commented 3 weeks ago

I've now added two additional AddRebus overloads that do what you requested 🙂

It's out in Rebus.ServiceProvider 10.2.0 which is available on NuGet.org now.

srogovtsev commented 3 weeks ago

Thank you so much, and sorry for not getting back with that PR I've promised.

mookid8000 commented 3 weeks ago

@srogovtsev no worries - it wasn't too hard 🙂