By using the Azure service bus topics and Subscriptions - I am able to pass message between the two systems. But however sometimes, I get this lock expired exception. How to avoid it ?
Exception - Message handler encountered an exception Microsoft.Azure.ServiceBus.MessageLockLostException: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue.
Below is the code :
static async Task MainAsync()
{
TokenProvider tokenProvider = TokenProvider.CreateManagedIdentityTokenProvider();
subscriptionClient = new SubscriptionClient(serviceBusNamespace, topicName, subscriptionName,
tokenProvider, receiveMode: ReceiveMode.PeekLock);
// Register subscription message handler and receive messages in a loop
RegisterOnMessageHandlerAndReceiveMessages();
Console.Read();
await subscriptionClient.CloseAsync();
}
static void RegisterOnMessageHandlerAndReceiveMessages()
{
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
MaxConcurrentCalls = 1,
AutoComplete = false
};
// Register the function that processes messages.
subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
}
static async Task ProcessMessagesAsync(Message message, CancellationToken token)
{
// Process the message recieved.
Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
}