question

Asif-1234 avatar image
0 Votes"
Asif-1234 asked MayankBargali-MSFT commented

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

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
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

anonymous user-1234 What happens when you debug your code using IIS Express? Did you log the exception when it runs on your IIS server. What exception do you observe?

0 Votes 0 ·

@MayankBargali-MSFT I am calling above handler in an async API call. When I call that api its rais following exception. "An asynchronous module or handler completed while an asynchronous operation was still pending." But in development mode it is working fine.

.

0 Votes 0 ·

anonymous user-1234 As per the exception shared it looks like the issue with the async call. Can can confirm on the stack trace if the exception is coming from the service bus SDK.

0 Votes 0 ·

1 Answer

Asif-1234 avatar image
0 Votes"
Asif-1234 answered MayankBargali-MSFT commented

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();
         }
     }
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

anonymous user-1234 Thanks for posting the answer. Feel free to "Accept as answer" so that it can help others in the community looking for help on similar topics.

0 Votes 0 ·