Unable to recieve messages from Azure when using Azure Messaging Service Azure.Messaging.ServiceBus 7.2.1.0

Asif 1 Reputation point
2021-07-26T08:42:10.683+00:00

Hi,
I am using .net and using latest version of Azure Messaging Service Azure.Messaging.ServiceBus 7.2.1. When I deploy my app on local IIS, I am unable to receive messages from Azure.

Here is my code:

static async Task MessageHandler(ProcessMessageEventArgs args)
        {
            await args.CompleteMessageAsync(args.Message);
            string body = args.Message.Body.ToString();
        }
        static Task ErrorHandler(ProcessErrorEventArgs args)
        {
            Console.WriteLine(args.Exception.ToString());
            LogError(args.Exception, ErrorCode.AzureServiceBusListner);
            return Task.CompletedTask;
        }
        static ServiceBusClient client;
        static ServiceBusProcessor processor;
        static async Task RecieveMessage()
        {
            client = new ServiceBusClient(sbConnectionString);
            processor = client.CreateProcessor(sbQueueName, new ServiceBusProcessorOptions());
            try
            {
                processor.ProcessMessageAsync += MessageHandler;
                processor.ProcessErrorAsync += ErrorHandler;
                await processor.StartProcessingAsync();
                await processor.StopProcessingAsync();
            }
            finally
            {
                await processor.DisposeAsync();
                await client.DisposeAsync();
            }
        }
Azure Service Bus
Azure Service Bus
An Azure service that provides cloud messaging as a service and hybrid integration.
623 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Asif 1 Reputation point
    2021-08-05T08:07:08.33+00:00

    I have resolved my issue. Here is my final code:

    My Issue is resoved here is my final code:

        static ServiceBusClient client;
        static ServiceBusProcessor processor;
        async Task RecieveMessage()
        {
            var options = new ServiceBusProcessorOptions
            {
                AutoCompleteMessages = false,
                MaxConcurrentCalls = 1
            };
            client = new ServiceBusClient(sbConnectionString);
             processor = client.CreateProcessor(sbQueueName, new ServiceBusProcessorOptions());
            try
            {
                processor.ProcessMessageAsync += MessageHandler;
                processor.ProcessErrorAsync += ErrorHandler;
                async Task MessageHandler(ProcessMessageEventArgs args)
                {
                    await args.CompleteMessageAsync(args.Message);
                     string body = args.Message.Body.ToString();
                      long MessageSequenceNumber = args.Message.SequenceNumber;
                               // complete the message. messages is deleted from the queue. 
    
                }
    
                //handle any errors when receiving messages
                Task ErrorHandler(ProcessErrorEventArgs args)
                {
                    Log("in ErrorHandler()");
                    Console.WriteLine(args.Exception.ToString());
                    LogError(args.Exception, ErrorCode.AzureServiceBusListner);
                    return Task.CompletedTask;
                }
                await processor.StartProcessingAsync();//.GetAwaiter().GetResult();
                 Console.WriteLine("Wait for a minute and then press any key to end the processing");
    
                //await processor.StopProcessingAsync();//.GetAwaiter().GetResult();
    
            }
    
           finally
            {
                //await processor.DisposeAsync();
                //await client.DisposeAsync();
            }
        }
    

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.