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

Handler of all failed messages by handling IFailed<object> #22

Closed jens-jonsson closed 4 years ago

jens-jonsson commented 5 years ago

Trying to create a handler to handle all failed messages by implementing IHandleMessages<IFailed<object>>.

Documentation suggests that implementing IHandleMessages<object> will call the handler for all messages but my constraint of IFailed does not seem to create a match when resolving handlers.

I am on .net core using default IServiceProvider. Checking assembly scanning shows that my handler gets registered but not called for failed messages.

mookid8000 commented 5 years ago

I'm not sure if that will work 😁

First thing to look for would be what your IoC container returns when resolving a list of IHandleMessages<IFailed<object>>

jens-jonsson commented 5 years ago

Sorry should have been more clear on that.

Calling IServiceProvider.GetServices<IHandleMessages<IFailed<object>>>() returns a list containing an instance of my registered handler.

mookid8000 commented 5 years ago

Calling IServiceProvider.GetServices<IHandleMessages<IFailed<object>>>() returns a list containing an instance of my registered handler.

oh sorry, what I meant was this: Try and see what the container returns when resolving a list of IHandleMessages<IFailed<YourMessageType>>

jens-jonsson commented 5 years ago

IServiceProvicer.GetServices<IHandleMessages<IFailed<PublishEntityCommand>>> works fine too, returning a list with my handler in it.

Base problem seems to be that stacking generics like IMessageHandler<T> where T is IFailed<U> does not work as I thought.

I guess if IFailed<> implemented e.g. a new marker interface IFailed I could use IHandleMessages<IFailed> and then work my way to what type the wrapped message is and what exception lies behind the failure.

Basically I am looking for a way to intercept all failed messages (after all retries have failed) regardless of type to be able to send it to telemetry for tracking. Much just like the example of using IFailFastChecker enables you to hook into the retry mechanism before the message fails permanently.

Decorating IErrorHandler works but context is lost and by that also our original correlationId.

Tsjunne commented 4 years ago

Made a pull request with a proposed solution

mookid8000 commented 4 years ago

Fixed in Rebus.ServiceProvider 5.0.2 🙂 Thanks @Tsjunne for your contribution! 👍