Request wrapper of multiple commands - routing question

Using NServiceBus 7, I’d like to know if it is possible to use a sort of wrapper request of multiple commands where the commands are of different interface type which can be sent once and routed to multiple endpoints based on the interfaces…i.e.

class WrapperRequestNotACommand
  : FirstCommand, SecondCommand
{
  object SomeProperty { get; set; }
}

I’d like to a controller action receive WrapperRequestNotACommand and just do a MessageSession.Send() with the two command interfaces FirstCommand and SecondCommand then routed to two different endpoints.

Currently I receive an exception about no routing specified for the WrapperRequestNotACommand type. Is what I’m trying to do possible?

Hey @simonfox

I think that’s currently not possible as routing looks up the destination for the message object’s actual type, so you’d have to map the wrapper type to an instance of the target message type. You can specify a message’s destination via send options though.

Cool thanks Tim.

Was this possible in the past? I knew I’d seen it somewhere - Service-Oriented API implementations

I don’t think it was but I’m not absolutely sure though. The blog post you mentioned wouldn’t need this to work either as it does a local send. Therefore it will only send the single message to the same endpoint and due to regular inheritance, NServiceBus will invoke all handlers for the specified interfaces implemented by the message. This approach works when individual services deploy handlers directly into the “shared” endpoint which does the SendLocal.

If you want to put these handlers into their dedicated endpoints, I think you’d be better off using events instead. By publishing the WrapperRequestNotACommand, every endpoint which has a handler for WrapperRequestNotACommand, FirstCommand and SecondCommand (some renaming would be appropriate in this case) would be invoked by the event without the need to manually send multiple messages. I’d argue this is also more aligned with the general recommendation to use events for communicating domain events to other services (so the publisher doesn’t need to know about them). See Messages, events, and commands • NServiceBus • Particular Docs for some more documentation on this. In the context of that blog post, you’d probably change the ShoppingOrder command to an OrderPlaced.

1 Like