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();
}
}