rebus-org / Rebus.ServiceProvider

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

Starting the bus on ASP.NET Core startup #25

Closed CzechsMix closed 4 years ago

CzechsMix commented 4 years ago

I've noticed that using the method of acting on the bus in the request pipeline, the bus doesn't start until the application actually receives a request and builds the pipeline. (I suspect it is also executing this step on each request, but have not yet taken the time to confirm)

I think the correct work around is to wrap IBus in some IHostedService (in order to subscribe/unscrubribe when starting/stopping as needed). Should the documentation be amended to include instructions for this, or is it worth providing a Rebus "native" way of supporting services.AddHostedService<>() ?

mookid8000 commented 4 years ago

I think actually this is because of the new two-step semi-idiomatic way of registering and starting things in .NET Core, which usually goes like this:

First you

services.Add****

to make the necessary registrations in the IoC container, and then you

app.Use***

to activate it.

With Rebus, it goes like this:

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

and

app.UseRebus();
mookid8000 commented 4 years ago

Did this solve your issue?

Please let me know if it didn't. 🙂

CzechsMix commented 4 years ago

I wrapped Rebus in a custom service that I registered as a IHostedService and it solved my issue if the app was set to start automatically in IIS. I no longer had to have an app.UserRebus statement.

alternatively, something like Pinging a health check in my release pipeline also worked.