Guest User

synchronous send/reply in generic host

a guest
Feb 23rd, 2012
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. Handler constructer
  2.  
  3. ConstructorFunction(bus)
  4. {
  5. Bus = bus
  6. }
  7.  
  8. code in the handle function.
  9. // sent the message to the bus and wait for the reply
  10.  
  11. IMessage response = null;
  12.  
  13. var synchronousHandle = Bus.Send(service2queue, requestMessage)
  14. .Register(
  15. (AsyncCallback)delegate(IAsyncResult asyncResult)
  16. {
  17. // Callback block for reply message
  18. // Reply message received
  19. NServiceBus.CompletionResult completionResult = asyncResult.AsyncState as NServiceBus.CompletionResult;
  20. if (completionResult != null && completionResult.Messages.Length > 0)
  21. {
  22. // Always expecting one IMessage as reply
  23. response = completionResult.Messages[0];
  24. }
  25. },
  26. null);
  27.  
  28.  
  29. // block the current thread till the reply received.
  30. synchronousHandle.AsyncWaitHandle.WaitOne();
  31.  
  32. Bus.Send(request).Register(asyncCallback, state)
  33.  
  34. Callback only fires on first response, then is cleaned up to prevent memory leaks.
  35. Doesn’t survive restarts – not suitable for server-side
  36.  
  37. service1 gets a notification about messageA
  38. service1 sends message requestMessage to service2
  39. service2 replies with message responseMessage to service1
  40. service1 handles responseMessage and continues processing
Advertisement
Add Comment
Please, Sign In to add comment