Skip to content

Sending and Recieving messages from Azure Service Bus.

Notifications You must be signed in to change notification settings

joerivanarkel/AzureServiceBus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

← Return to AZ-204

.NET

Azure Service Bus

Sending and Receiving messages from Azure Service Bus To work with a Azure Service Bus, you will have to use the ServiceBusClient to interact with this resource.

Sending a Message to a Queue

To send a message to the Queue, you have to use the ServiceBusClient to create a Sender with the queue name. Secondly you have to create a ServiceBusMessage, with the message for the body. Then you await the SendMessageAsync() method and finally close the connection with the CloseAsync() method.

var serviceBusSender = _serviceBusClient.CreateSender("testqueue");
var sendmessage = new ServiceBusMessage("Hello, World!");
await serviceBusSender.SendMessageAsync(sendmessage);
await serviceBusSender.CloseAsync();

Receiving a Message from a Queue

Akin to the sender, you must first create a Receiver using the ServiceBusClient. Then using the RecievedMessageAsync() method to fetch the message, which returns the ServiceBusReceivedMessage type object. Then I establish that the received message isn't empty and then dequeue the message with the CompleteMessageAsync(), giving it the exact message. Finally is close the connection with the CloseAsync() method.

var serviceBusReciever = _serviceBusClient.CreateReceiver("testqueue");
var recievedmessage = await serviceBusReciever.ReceiveMessageAsync();

if (recievedmessage != null)
{
  await serviceBusReciever.CompleteMessageAsync(recievedmessage);
}

await serviceBusReciever.CloseAsync();

About

Sending and Recieving messages from Azure Service Bus.

Topics

Resources

Stars

Watchers

Forks

Languages