IHandleSagaNotFound not triggered

Hello!

I’ve been trying to implement IHandleSagaNotFound interface to catch and log all commands and messages that can’t be handled by Saga or handlers. The problem is that I can’t seem to get the Handle on IHandleSagaNotFound to be executed when an unknown ICommand is sent to the endpoint.

To test it outside our production code, I

  1. downloaded NServiceBus sagas: Saga basics • Particular Docs
  2. Added “UnknownCommand : ICommand” in the Messages project
  3. In the ClientUI project Routed the UnknownCommand to Sales endpoint
  4. Created “SagaNotFoundHandler : IHandleSagaNotFound” in the Sales project (Sagas Not Found • NServiceBus • Particular Docs)
  5. Sent the UnknownCommand from the ClientUI
  6. All I get in the Sales project console is
15:41:35.051 INFO  Immediate Retry is going to retry message '153c6f30-669c-4500-b092-af0100d11e2d' because of an exception:
System.InvalidOperationException: No handlers could be found for message type: Messages.UnknownCommand

I had an assumption that IHandleSagaNotFound’s Handle method should be called instead.

Hi

I had an assumption that IHandleSagaNotFound’s Handle method should be called instead.

This is not the case. The not found handler is only called for messages that both

  • are recognized (implement IMessage) or match the convention
  • are mapped to a saga type that handles it

The exception you get is what gets raised when the latter condition is not met. Is the NServiceBus way of logging that case not enough for your requirements?

Szymon

Ah, so IHandleSagaNotFound is called only when an IMessage handling instance of Saga for some reason is missing in runtime?

That is a good point, we have behaviors for custom logging, will check if InvalidOperationException is not excluded for some reason.

Ah, so IHandleSagaNotFound is called only when an IMessage handling instance of Saga for some reason is missing in runtime?

Yeah. The most common scenario is that a reply to a message send by a saga arrives after a given saga has already been completed. Note, that the behavior is different for timeout messages. If these arrive and the saga instance is no longer there, the message is silently dropped.

Thanks.
In the end I created a new Behavior with IIncomingLogicalMessageContext. I added try/catch in the Invoke method to handle InvalidOperationException.