Sending and Receving messages to Azure Service Bus using ASP.NET framework 4.8

Chandrakant Pawle 101 Reputation points
2024-05-10T05:44:53.6933333+00:00

Hello

Trying to send and recieve message using ASP.NET example to Azure Service Bus. Pls share the code if possible.

https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/servicebus/Azure.Messaging.ServiceBus/samples/Sample01_SendReceive.md

Have tried above URL + code which gives error in below line

ServiceBusSender sender = client.CreateSender(queueName);

Using currtenly Azure.Messaging.ServiceBus 7.17.5 client library for this example

Thanks in advance

Regards

Chandrakant

Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
621 questions
{count} votes

1 answer

Sort by: Most helpful
  1. hossein jalilian 6,830 Reputation points
    2024-05-10T06:16:02.76+00:00

    Thanks for posting your question in the Microsoft Q&A forum.

    Here's an example of how you can send and receive messages:

    1. Install the Azure.Messaging.ServiceBus NuGet package in your ASP.NET Core project.
    2. In your Startup.cs file, add the following code to register the ServiceBusClient as a singleton service:
         public void ConfigureServices(IServiceCollection services)
         {
             services.AddSingleton(serviceProvider =>
             {
                 var connectionString = Configuration.GetConnectionString("ServiceBusConnectionString");
                 return new ServiceBusClient(connectionString);
             });
         }
         
      
    3. Create a new class, for example, ServiceBusQueueService.cs, and implement the logic for sending and receiving messages:
         using Azure.Messaging.ServiceBus;
         public class ServiceBusQueueService
         {
             private readonly ServiceBusClient _serviceBusClient;
             private readonly string _queueName = "myqueue"; 
             public ServiceBusQueueService(ServiceBusClient serviceBusClient)
             {
                 _serviceBusClient = serviceBusClient;
             }
             public async Task SendMessageAsync(string message)
             {
                 var sender = _serviceBusClient.CreateSender(_queueName);
                 var serviceBusMessage = new ServiceBusMessage(message);
                 await sender.SendMessageAsync(serviceBusMessage);
             }
             public async Task ReceiveMessagesAsync()
             {
                 var receiver = _serviceBusClient.CreateReceiver(_queueName);
                 var messages = await receiver.ReceiveMessagesAsync(maxMessages: 10);
                 foreach (var message in messages)
                 {
                     Console.WriteLine($"Received message: {message.Body}");
                     await receiver.CompleteMessageAsync(message);
                 }
             }
         }
         
      
    4. In your controller or service where you want to send or receive messages, inject the ServiceBusQueueService and call the respective methods:
    public class MyController : Controller
    {
        private readonly ServiceBusQueueService _serviceBusQueueService;
        public MyController(ServiceBusQueueService serviceBusQueueService)
        {
            _serviceBusQueueService = serviceBusQueueService;
        }
        public async Task<IActionResult> SendMessage(string message)
        {
            await _serviceBusQueueService.SendMessageAsync(message);
            return Ok();
        }
        public async Task<IActionResult> ReceiveMessages()
        {
            await _serviceBusQueueService.ReceiveMessagesAsync();
            return Ok();
        }
    }
    
    

    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.