In this article, I will explain and provide the working code to pusblish message to Azure service bus using C# console app.
First of all install the Server Bus package and include below ref
using Azure.Messaging.ServiceBus;
Define service bus connection string and queue name
private const string
serviceBusConnectionString = "Copy and paste service bus connection string";
private const string queueName = "demo-servicebusqueue";
Define variables to use
private const int numOfMessages = 200;
static ServiceBusClient client
= default!;
static ServiceBusSender sender
= default!;
static ServiceBusProcessor
processor = default!;
Below is the main method calls Message publisher method which generates message and push to service bus
public static async Task Main(string[] args)
{
try
{
await MessagePublisher();
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
Define publisher method, in this case, which generates message
private static async Task<string> MessagePublisher()
{
// create service bus client
client
= new
ServiceBusClient(serviceBusConnectionString);
// Create message sender
sender
= client.CreateSender(queueName);
// create service message batch
using ServiceBusMessageBatch
messageBatch = await sender.CreateMessageBatchAsync();
var msg = "";
for (int i =1; i <=
numOfMessages; i++)
{
msg= $"{{\"id\": {i},\"createdOn\":
\"2023-04-05T13:04:57\",\"description\": \"This is
very good product with 1 year wanranty\",\"isDone\":
false}}";
// add message to batch
if
(!messageBatch.TryAddMessage(new ServiceBusMessage(msg)))
{
throw new Exception($"An exception has been
thrown");
}
}
try
{
// finally send message to azure service bus
await
sender.SendMessagesAsync(messageBatch);
Console.WriteLine($"A batch of {numOfMessages} messages has been
published to the queue.");
}
finally
{
await sender.DisposeAsync();
await client.DisposeAsync();
}
return "All messages have
been published to the queue";
}
Complete code
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.Amqp.Framing;
namespace AzServiceBus
{
public class Program
{
private const string
serviceBusConnectionString = "Copy and paste service bus connection string";
private const string queueName = "demo-servicebusqueue";
private const int numOfMessages = 200;
static ServiceBusClient client
= default!;
static ServiceBusSender sender
= default!;
static ServiceBusProcessor
processor = default!;
public static async Task Main(string[] args)
{
try
{
await MessagePublisher();
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
private static async Task<string> MessagePublisher()
{
client
= new
ServiceBusClient(serviceBusConnectionString);
sender
= client.CreateSender(queueName);
using ServiceBusMessageBatch
messageBatch = await sender.CreateMessageBatchAsync();
var msg = "";
for (int i =1; i <=
numOfMessages; i++)
{
msg= $"{{\"id\": {i},\"createdOn\":
\"2023-04-05T13:04:57\",\"description\": \"This is
very good product with 1 year wanranty\",\"isDone\":
false}}";
if
(!messageBatch.TryAddMessage(new ServiceBusMessage(msg)))
{
throw new Exception($"An exception has been
thrown");
}
}
try
{
await
sender.SendMessagesAsync(messageBatch);
Console.WriteLine($"A batch of {numOfMessages} messages has been
published to the queue.");
}
finally
{
await sender.DisposeAsync();
await client.DisposeAsync();
}
return "All messages
have been published to the queue";
}
}
}