Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. // create client
  2. IQueueClient queueClient = new QueueClient("Endpoint=sb://sb-doubledecker-ric-eun.servicebus.windows.net/;SharedAcce...", "myQueue");
  3.  
  4. // send a message to a queue
  5. var message = new Message(Encoding.UTF8.GetBytes("Message content"));
  6. \!h await queueClient.SendAsync(message);
  7.  
  8. // receive messages from queue
  9. // Configure the message handler options in terms of exception handling, number of concurrent messages to deliver, etc.
  10. var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
  11. {
  12. // Maximum number of concurrent calls
  13. MaxConcurrentCalls = 1,
  14. // Automatically complete the messages after returning from user callback.
  15. // False indicates the complete operation is handled by the user callback.
  16. AutoComplete = false
  17. };
  18.  
  19. // Register the function that processes messages.
  20. \!h queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
  21.  
  22. // message handler
  23. static async Task ProcessMessagesAsync(Message message, CancellationToken token)
  24. {
  25. // sequence
  26. message.SystemProperties.SequenceNumber;
  27.  
  28. // body
  29. Encoding.UTF8.GetString(message.Body);
  30.  
  31. // Complete the message so that it is not received again.
  32. // This can be done only if the queue Client is created in ReceiveMode.PeekLock mode (which is the default).
  33. \!h await queueClient.CompleteAsync(message.SystemProperties.LockToken);
  34. }
  35.  
  36. // handle exceptions
  37. static Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
  38. {
  39. // encountered exception
  40. exceptionReceivedEventArgs.Exception;
  41.  
  42. return Task.CompletedTask;
  43. }
  44.  
  45. // close client
  46. await queueClient.CloseAsync();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement