Publish and Consume Azure Service bus queues pro-grammatically in C#

You can easily publish messages in to the queue and subscribe messages from queue pro grammatically using C#
Publish messages in to queues

var connectionString = "Endpoint=sb://rack-messaging.servicebus.windows.net/;SharedAccessKeyName=messagecollectionlistener;SharedAccessKey=DnentJvFthm13IWicMJxrM68LK3c+zEeQb6QlQn71SA=;EntityPath=messagecollection";
var client = QueueClient.CreateFromConnectionString(connectionString);
var lastKey = ' ';
var count = 1;
while (lastKey != 'q')
{
var body = $"Message {count++}";
var message = new BrokeredMessage(body);
client.Send(message);
Console.WriteLine($"Sent {message.MessageId}");
lastKey = Console.ReadKey().KeyChar;
}

Consume

var connectionString = "Endpoint=sb://rack-messaging.servicebus.windows.net/;SharedAccessKeyName=messagecollectionlistener;SharedAccessKey=DnentJvFthm13IWicMJxrM68LK3c+zEeQb6QlQn71SA=;EntityPath=messagecollection";
var client = QueueClient.CreateFromConnectionString(connectionString);
client.OnMessage(message => {
Console.WriteLine($"Message : {message.MessageId} Message Body : {message.GetBody()}");
});
Console.ReadLine();

Connection string you can copy from here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s