Search code examples
c#masstransit

Masstransit: difference between GetPublishSendEndpoint and GetSendEndpoint


What is the difference between: calling the generic GetPublishSendEndpoint() and providing a type for the message being sent and calling the GetSendEndpoint() and providing a URI parameter?

When would you use one over another?

To my understanding Sending (to a queue) and publishing (to an exchange) are two very different operations, so the idea of a PublishSend endpoint is confusing to me, and the documentation doesn't help.


Solution

  • When using RabbitMQ, if you use MassTransit's default routing, you can almost always use publish 100% of the time. As in, just call Publish<T>. I follow this advice almost 100% of the time, and with good reason. It's meant to make it easy and avoids the need to configure send addresses because "OMG commands have to be sent." That's nonsense, and I talk about it in several videos.

    My previous answer on this subject is also relevant, stop trying to build routing around a framework that already has routing.

    And GetPublishSendEndpoint<T> is really just an internal methods used by Publish<T>, which should make sense as it is on IPublishEndpoint (and not ISendEndpointProvider).

    Also, you should be using one of those two interfaces in consumer dependencies, whereas in the consumer you should always use ConsumeContext, to produce messages.

    In summary:

    1. In a consumer, using ConsumeContext to produce messages (publish, send, respond)
    2. In a consumer dependency, using IPublishEndpoint or ISendEndpointProvider.
    3. Outside of a consumer, use IPublishEndpoint or ISendEndpointProvider whenever possible.
    4. Last resort, and I mean the absolute last resort, use IBus.

    This is also outlined in the documentation.