Matt Whetton
Matt Whetton

Reputation: 6786

How to throw an exception that forces message to error queue in NServiceBus

Is there a way in NServiceBus that you can throw an exception (or do something else) within a handler that avoids the retries, and automatically routes the message to the error queue?

A use case would when the message being handled is correctly formed, but has an invalid value, like an empty string. In this case retrying is never going to change the content of the message, so retrying is pointless.

Thanks

Matt

Upvotes: 2

Views: 1510

Answers (1)

David Boike
David Boike

Reputation: 18635

You might want to check out the API for creating a custom recoverability policy which specifically covers NServiceBus 6.0, or for earlier versions, the more general recoverability article that you can change to your specific version of NServiceBus. (Before NServiceBus 6.0 it's not quite as easy to configure.)

However, I would ask how those messages are getting in there in a completely invalid state in the first place. Validation and recovery are technically separate concerns. Validation logic can be shared similar to the message assemblies and run on the sending and receiving sides. Then the correct action to take when a message winds up in the error queue is to update the validation logic for the unforeseen condition.

I would argue it's probably not worth building any machinery to prevent retries from ever occurring. Better to build validation to catch the problem at the source. Once in awhile a bad message will slip through, but it's probably not the end of the world so just let the retries happen.

Upvotes: 2

Related Questions