Skip to content

Instantly share code, notes, and snippets.

@mikepfeiffer
Created October 27, 2019 06:19
Show Gist options
  • Save mikepfeiffer/1d1d04f014b9929be7fcbc3382bb78a1 to your computer and use it in GitHub Desktop.
Save mikepfeiffer/1d1d04f014b9929be7fcbc3382bb78a1 to your computer and use it in GitHub Desktop.
Service Bus Console App .NET Core Sample
using Microsoft.Azure.ServiceBus;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SBQuickStart
{
class Program
{
static IQueueClient queueClient;
static void Main(string[] args)
{
var serviceBusConnectionString = string.Empty;
var queueName = string.Empty;
for (var i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "-ConnectionString":
Console.WriteLine($"ConnectionString: {args[i+1]}");
serviceBusConnectionString = args[i + 1]; // Alternatively enter your connection string here.
break;
case "-QueueName":
Console.WriteLine($"QueueName: {args[i+1]}");
queueName = args[i + 1]; // Alternatively enter your queue name here.
break;
}
}
if (!string.IsNullOrEmpty(serviceBusConnectionString) && !string.IsNullOrEmpty(queueName))
MainAsync(serviceBusConnectionString, queueName).GetAwaiter().GetResult();
else
{
Console.WriteLine("Specify -ConnectionString and -QueueName to execute the example.");
Console.ReadKey();
}
}
static async Task MainAsync(string serviceBusConnectionString, string queueName)
{
const int numberOfMessages = 10;
queueClient = new QueueClient(serviceBusConnectionString, queueName);
Console.WriteLine("======================================================");
Console.WriteLine("Press any key to exit when complete");
Console.WriteLine("======================================================");
// Register QueueClient's MessageHandler and receive messages in a loop
// RegisterOnMessageHandlerAndReceiveMessages();
// Send Messages
await SendMessagesAsync(numberOfMessages);
Console.ReadKey();
await queueClient.CloseAsync();
}
static void RegisterOnMessageHandlerAndReceiveMessages()
{
// Configure the MessageHandler Options in terms of exception handling, number of concurrent messages to deliver etc.
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
// Maximum number of Concurrent calls to the callback `ProcessMessagesAsync`, set to 1 for simplicity.
// Set it according to how many messages the application wants to process in parallel.
MaxConcurrentCalls = 1,
// Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
// False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
AutoComplete = false
};
// Register the function that will process messages
queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}
static async Task ProcessMessagesAsync(Message message, CancellationToken token)
{
// Process the message
Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
// Complete the message so that it is not received again.
// This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default).
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
// Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed.
// If queueClient has already been Closed, you may chose to not call CompleteAsync() or AbandonAsync() etc. calls
// to avoid unnecessary exceptions.
}
// Use this Handler to look at the exceptions received on the MessagePump
static Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
{
Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}.");
var context = exceptionReceivedEventArgs.ExceptionReceivedContext;
Console.WriteLine("Exception context for troubleshooting:");
Console.WriteLine($"- Endpoint: {context.Endpoint}");
Console.WriteLine($"- Entity Path: {context.EntityPath}");
Console.WriteLine($"- Executing Action: {context.Action}");
return Task.CompletedTask;
}
static async Task SendMessagesAsync(int numberOfMessagesToSend)
{
try
{
for (var i = 0; i < numberOfMessagesToSend; i++)
{
// Create a new message to send to the queue
var messageBody = $"Message {i}";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
// Write the body of the message to the console
Console.WriteLine($"Sending message: {messageBody}");
// Send the message to the queue
await queueClient.SendAsync(message);
}
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment