Reputation: 812
I have an Azure function which I just updated to look like this:
public void Run([ServiceBusTrigger("xxxx", Connection = "ServiceBusConnectionString", IsSessionsEnabled = true)] Message message, ILogger log, IMessageSession messageSession)
I did this so I could do this:
var rec = messageSession as MessageReceiver;
if(rec != null)
{
rec.RetryPolicy = Microsoft.ServiceBus.RetryPolicy.NoRetry;
}
_event.PushEvent(new Event
{
EventName = $"{message.MessageId} - {message.SystemProperties.DeliveryCount}",
EventData = string.Empty,
DateOfEvent = DateTime.Now,
EventLevel = 1,
System = Systems.CRMSQL
}).Wait();
// Manually close message so it won't requeue
messageSession.CloseAsync().Wait();
My event logging is showing the same message being processed over and over
EventName EventData EventLevel DateOfEvent
5f353e313f104c298bdffa08030d9daa - 1 1 2020-09-03 01:17:47.903
5f353e313f104c298bdffa08030d9daa - 1 1 2020-09-03 01:17:46.083
5f353e313f104c298bdffa08030d9daa - 1 1 2020-09-03 01:17:45.227
Why is my function pulling the same message over and over, without the delivery count increasing, even though I am manually closing the message? It did this before I added this code, this is the issue I was trying to resolve.
My logs confirm we called the function to push into the service bus once. I log pushing as an event, there is one event log. The graph in Azure shows a jagged climb to 300 messages in the queue.
Upvotes: 0
Views: 73
Reputation: 26057
// Manually close message so it won't requeue messageSession.CloseAsync().Wait()
The function will automatically complete the incoming message once it's done. If you're closing the receiver before incoming message was completed, the message will be redelivered maxDeliveryCount
times and you'll see duplicates.
Not sure what _event.PushEvent
is, but if it's your code, you should elaborate what it does. Additionally, don't force asynchronous code to run with .Wait()
. Rather use async/await
.
Upvotes: 1