I am trying to publish a message from the catch block in the Rebus message handlers without affecting the retry mechanism of rebus.
My intent is,
- Process the message in message handlers.
- In case of an error, catch it, and publish some error event using the same bus.
- Just after publishing "throw" the caught exception so that rebus ACK/NACK is placed automatically for the retry/re-delivery of the message.
I cannot achieve the above because if an exception is thrown from the rebus message handlers, that message is automatically flagged for re-delivery and the whole pipeline transaction is rolled back. This negates the second point above as when transaction is rolled back, the message I sent to be published is rolled back as well. Is there a way I could achieve this i.e. publishing the message as well as auto-retry ability. My message handler code is below.
public Task Handle(SomeMessage message)
{
try
{
//Some code that may result in an error
}
catch (Exception ex)
{
_bus.PublishSomeMessageErrorEvent(ex);
// throw an error and let Rebus retry the delivery.
throw;
}
return Task.CompletedTask;
}
I Also tried working with second level retries so that when failed message comes in the IHandleMessages<IFailed> handler I would simply publish the message with _bus.PublishSomeMessageErrorEvent(...) method but while setting the second level retry I received exception while starting the bus.
_bus = Configure.With(...)
.Options(r=>r.SimpleRetryStrategy(secondLevelRetriesEnabled:true))
.ConfigureSqlServerTransportFromAppConfig()
.Logging(c => c.Log4Net())
.Start();
The exception Attempted to register primary -> Rebus.Retry.Simple.SimpleRetryStrategySettings, but a primary registration already exists: primary -> Rebus.Retry.Simple.SimpleRetryStrategySettings