Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1.     internal class Program
  2.     {
  3.         private const string serviceBusConnectionString = "XXX";
  4.  
  5.         private const string queueName = "myQueue";
  6.         private static IQueueClient queueClient;
  7.  
  8.         public static void Main(string[] args)
  9.         {
  10.             mainAsync().GetAwaiter().GetResult();
  11.         }
  12.  
  13.         private static async Task mainAsync()
  14.         {
  15.             queueClient = new QueueClient(serviceBusConnectionString, queueName, ReceiveMode.PeekLock)
  16.             {
  17.                 //PrefetchCount = 0
  18.             };
  19.  
  20.             Console.WriteLine("======================================================");
  21.             Console.WriteLine("Press ENTER key to exit after receiving all the messages.");
  22.             Console.WriteLine("======================================================");
  23.  
  24.             // Register QueueClient's MessageHandler and receive messages in a loop
  25.             registerOnMessageHandlerAndReceiveMessages();
  26.  
  27.             Console.ReadKey();
  28.  
  29.             await queueClient.CloseAsync();
  30.         }
  31.  
  32.         private static void registerOnMessageHandlerAndReceiveMessages()
  33.         {
  34.             // Configure the MessageHandler Options in terms of exception handling, number of concurrent messages to deliver etc.
  35.             MessageHandlerOptions messageHandlerOptions = new MessageHandlerOptions(exceptionReceivedHandler)
  36.             {
  37.                 // Maximum number of Concurrent calls to the callback `ProcessMessagesAsync`, set to 1 for simplicity.
  38.                 // Set it according to how many messages the application wants to process in parallel.
  39.                 MaxConcurrentCalls = 1,
  40.  
  41.                 // Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
  42.                 // False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
  43.                 AutoComplete = false,
  44.                 //MaxAutoRenewDuration = TimeSpan.FromDays(30)
  45.             };
  46.  
  47.             // Register the function that will process messages
  48.             queueClient.RegisterMessageHandler(processMessagesAsync, messageHandlerOptions);
  49.         }
  50.  
  51.         private static async Task processMessagesAsync(Message message, CancellationToken token)
  52.         {
  53.             // Process the message
  54.             Console.Write(
  55.                 $"Received message: {message.SystemProperties.SequenceNumber}. Remaining lock duration: {message.SystemProperties.LockedUntilUtc - DateTime.UtcNow}");
  56.  
  57.             Thread.Sleep(10000);
  58.             // Complete the message so that it is not received again.
  59.             // This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default).
  60.             await queueClient.CompleteAsync(message.SystemProperties.LockToken);
  61.  
  62.             Console.WriteLine(" - Complete!");
  63.  
  64.             // Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed.
  65.             // If queueClient has already been Closed, you may chose to not call CompleteAsync() or AbandonAsync() etc. calls
  66.             // to avoid unnecessary exceptions.
  67.         }
  68.  
  69.         private static Task exceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
  70.         {
  71.             Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}.");
  72.             ExceptionReceivedContext context = exceptionReceivedEventArgs.ExceptionReceivedContext;
  73.             Console.WriteLine("Exception context for troubleshooting:");
  74.             Console.WriteLine($"- Endpoint: {context.Endpoint}");
  75.             Console.WriteLine($"- Entity Path: {context.EntityPath}");
  76.             Console.WriteLine($"- Executing Action: {context.Action}");
  77.             return Task.CompletedTask;
  78.         }
  79.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement