Discard messages from failed requests

If a request fails for some reason (typically timeout), the reply will be retried by NServiceBus when it eventually arrives which leads to InvalidOperationException with the message “No handlers could be found for message type”.

I’m trying to discard it by creating a custom recoverability policy which uses RecoverabilityAction.Discard() if the exception is InvalidOperationException and the message contains "No handlers could be found for message type".

But that logic doesn’t seem to be called for the reply. Is my code incorrect, or can’t this scenario be handled by a custom policy?

Hi Jonas

Are NServiceBus.Callbacks involved in the scenario you are describing?

Regards
Daniel

Yes, Callbacks are activated.

Hi Jonas

I did verify and it seems to work, I created a draft PR for you to have a look and compare

        endpointConfiguration.Recoverability().CustomPolicy((config, context) =>
        {
            if (context.Exception is InvalidOperationException invalidOperationException && invalidOperationException.Message.StartsWith("No handlers could be found"))
            {
                return RecoverabilityAction.Discard("Callback no longer active");
            }
            return DefaultRecoverabilityPolicy.Invoke(config, context);
        });

Regards
Daniel