Whenever two things communicate, there is always a contract. When functions call each other the contract is the parameters that are required to call that function. With messaging the message is the contract. The coupling is towards the message, not the sender or receiver.
I'm not really sure what you're trying to achieve? You mention an API which is async and endpoint1
and endpoint2
.
First of all, there's asynchronous execution and asynchronous communication. The async
part in your example code is asynchronous execution of two methods that have the word await
in front of them. When we talk about sending messages, that's asynchronous communication. A message is put on the queue and then the code moves on and never looks back at the message. Even when you use the request/reply pattern, no code is actually waiting for a message.
You can wait for a message by blocking the thread, but I highly recommend you avoid that and not use the NServiceBus callback feature. If you think you have to, think again. If you still think so, read the red remarks on that page. If they can't convince you, contact Particular Software to have them explain another time why not. ;-)
It could be that you need a reply message for whatever reason. If you build some website using SignalR (for example) and you want to inform the user on the website when a message returned and some work was completed, you can wait for a reply message. The result is that the website itself becomes an endpoint.
So if the website is EndpointA
and it sends a message to EndpointB
, it is possible to reply to that message. EndpointA
would then also need a message handler for that message. If EndpointB
first needs to send a message to EndpointC
, which in turn responds to EndpointB
and only then it replies back to EndpointA
, NServiceBus can't easily help. Not because it's impossible, but because you probably need another solution. EndpointA
should probably not be waiting for that many endpoints to reply, so many things could go "wrong" and take too much time.
If you're interested to see how replies work in combination with SignalR and what not, you can check a demo I built for a presentation that has that.