38

What are the essential differences between publishing a message using Bus.Publish and sending a message using Bus.Send? I am looking to understand how they differ and also when I should choose to use one over the other.

Sean Kearon
  • 10,987
  • 13
  • 77
  • 93

3 Answers3

35

Publishing is used to notify multiple Subscribers of a particular event. A Publishing endpoint will have subscription storage to identify where to send messages to. Sending is typically used to issue a command to an endpoint. A command is telling the endpoint to do something and should not expect a reply(although you sometimes do want a reply and NSB supports this).

The reason you do not see a destination for Send() is that you specify the destination via configuration. In your app.config you will map message types(a whole assembly or a class) to a destination. When you do so, you do not have to provide the destination.

Adam Fyles
  • 6,030
  • 1
  • 23
  • 29
23

Bus.Publish: used when you don't know where the message is going (0 to many subscribers).
Bus.Send: when you are sending a message to a specific handler (client to server).

Gord
  • 1,805
  • 3
  • 17
  • 22
  • Okay, but then why do some Send() overloads not have a destination? Confused... – Sean Kearon Feb 08 '11 at 23:13
  • 8
    We take the semantic meaning a bit further. Publish is an event, and we treat events as "hey, something just happened. I don't care what you do with this information, but here you go", where as we treat Bus.Send as commands, which mean "Hey, I need you to do something, and I care about what happens as a result (and therefore know who I am sending it to)" – Matt Mar 05 '14 at 16:35
  • 2
    @SeanKearon Yes, you posted this 4 years ago, but for anyone else. The destination of the send command can be defined in configuration. You don't really want in your code configuration information, so you can use the simpler form of Send() which doesn't take the destination as a parameter if it is defined in your configuration. – XenoPuTtSs Nov 13 '14 at 13:50
1

ususally Context.Publish() is for publishing Event Type and Context.Send() is for Command Type

Vakar
  • 31
  • 4