Is it possible to Force a message to be posted to error queue without retries?

I am trying to add an authorization feature on top of NSB using mutators and behaviors

  • I have added an outgoing message mutator that adds the requesting users name to the message headers.
  • I have added an incoming logical message behavior that takes this value from the message header and checks if the user is authorized for the action. I am using an incoming logical message behavior, because the message is decorated with an attribute that determines what roles are authorized for it.
  • If the user is not authorized to perform the action, I will throw an UnauthorizedAccessException

When this type of exception is thrown, I would like to immediately fail the message and move it to the Error Q, not perform first and second level retries.

Looking through the documentation, it seems I should be able to handle this with a behavior… but I am not sure where/how. I read some posts that seemed to say the only way to short circuit it going to the error Q was by throwing a MessageDeserializationException… but since that is the first thing that happens to get a logical message, I am unsure if it’s possible.

What I have now works but the message is tried and throws an exception multiple times before going to the error Q. It rarely happens, but, when it does, I would like to skip the retries.

If anyone has any ideas, I’d like to hear it.

Thanx

Hi @macdonald-k,

You can do this by either defining a custom recoverability policy or registering the specific exception with unrecoverable exceptions.

Hi,

In general:

You can disable FLR:

You can also disable SLR:

But for your needs, as Sean Feldman mentions, you can configure your own custom policy as well.

Cheers,
Indu Alagarsamy
Particular Software

As you are throwing a specific exception, the following would skip any retries:

var recoverability = endpointConfiguration.Recoverability();
recoverability.AddUnrecoverableException<UnauthorizedAccessException>();

Please note that this API has been added to NServiceBus 6.2.0, so it requires 6.2.0 or higher.

awesome, thanx. That’s perfect.

Wow. Looks like I forgot that I already did this by adding

Defaults(s => s.AddUnrecoverableException(typeof(UnauthorizedAccessException)));

to the features constructor.