Difference between Bus.Publish and Bus.Send in NServiceBus?
Asked Answered
C

3

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.

Chang answered 8/2, 2011 at 21:29 Comment(0)
U
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.

Unmarked answered 9/2, 2011 at 0:58 Comment(3)
Thanks Adam - that makes sense not I see role that configuration plays.Chang
Err, the above comment should have read: Thanks Adam - that makes sense now I see role that the configuration plays.Chang
One of the overrides for Bus.Send is Bus.Send(string destination, message), so you can use Bus.Send to send to a specific queue.Alixaliza
C
24

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).

Cecil answered 8/2, 2011 at 21:51 Comment(3)
Okay, but then why do some Send() overloads not have a destination? Confused...Chang
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)"Illconditioned
@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.Butacaine
E
1

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

Edge answered 10/4, 2017 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.