We are using Azure Service Bus in our project and while reading messages from service bus topic/subscription. We are using ‘subscriptionClient.OnMessageAsync’ event in conjunction with ‘onMessageOptions.ExceptionReceived’. Let me write down the steps we followed to reproduce the issue we are facing.
- Create a service bus namespace with default config in the azure portal
- Create a topic inside it with default config in the azure portal
- Create a subscription inside it with default config in the azure portal
- Create a console app and paste the code added below
- Connect the service bus using Service Bus Explorer
- Run the console app
- Send a few test messages from service bus explorer & watch the console app window
- Though the messages are processed successfully every time the control is going inside the ‘ExceptionReceived’ method
class Program {
static void Main()
{
var subscriptionClient = SubscriptionClient.CreateFromConnectionString( servicebusendpointaddress", "topicname", subscriptionname", ReceiveMode.PeekLock);
var onMessageOptions = new OnMessageOptions();
onMessageOptions.ExceptionReceived += OnMessageError;
subscriptionClient.OnMessageAsync(OnMessageReceived, onMessageOptions);
Console.ReadKey();
}
private static void OnMessageError(object sender, ExceptionReceivedEventArgs e)
{
if (e != null && e.Exception != null)
{
Console.WriteLine("Hey, there's an error!" + e.Exception.Message + "\r\n\r\n");
}
}
private static async Task OnMessageReceived(BrokeredMessage arg)
{
await arg.CompleteAsync();
Console.WriteLine("Message processing done!");
}
}Message processing done!Hey, there's an error!The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. TrackingId:dd011e
d7-1037-4f1e-912b-c0c605eec60a_G1_B13,TimeStamp:5/18/2015 1:55:02 PM
Message processing done!
Hey, there's an error!The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. TrackingId:2d3aaf
a4-a6cc-48f3-bd85-63e476daef1b_G1_B13,TimeStamp:5/18/2015 1:55:04 PM
Are we missing something here?
Also one point to mention is that is we enable ‘autocomplete’ and remove the ‘await arg.CompleteAsync();’ then this is not happening.
var onMessageOptions = new OnMessageOptions() { AutoComplete = true};In both the cases the messages are being processed successfully & removed from the subscription immediately.