0

I have a C# code to send a message to the service bus topic as follows:

public class ServiceBusTopicsRepository : IServiceBusTopicsRepository
{
    private TopicClient _topicClient;

    public ServiceBusTopicsRepository(string connectionString, string entityPath)
    {
        _topicClient = new TopicClient(connectionString, entityPath);
    }

    public async Task AddMessageAsync(SyncJob job)
    {
        await _topicClient.SendAsync(CreateMessage(job));
    }

    private Message CreateMessage(SyncJob job)
    {
        var body = JsonSerializer.Serialize(job);
        var message = new Message
        {
            Body = Encoding.UTF8.GetBytes(body)
        };

        message.MessageId = "PK_RK";

        return message;
    }
}

On running the code, when it hits the breakpoint after line:

await _topicClient.SendAsync(CreateMessage(job));

I see message is not being added to the topic all the time when I execute the code.

DuplicateDetectionHistoryTimeWindow is 10 min

When I try to send message after 12 minutes, I see newly sent message is instantly ignored and dropped. Looking at this doc https://learn.microsoft.com/en-us/azure/service-bus-messaging/duplicate-detection, I see that if any new message is sent with MessageId that was logged during the time window (10 min), the message is reported as accepted (the send operation succeeds), but the newly sent message is instantly ignored and dropped. However, it looks like it ignoring the message even after 10 min.

I tried updating the MessageId to "PK_RK" + random GUID. I still see the same issue.

user989988
  • 3,006
  • 7
  • 44
  • 91
  • Are there any filters on the subscription(s): https://learn.microsoft.com/en-us/azure/service-bus-messaging/topic-filters#filters Also, is there an application consuming these messages out of the subscription(s)? It sounds like you might have multiple consumers / multiple instances of a consumer trying to lock the same message. This message can also come up if the consumer takes a long time to execute and the message lock expires. See this answer: https://stackoverflow.com/questions/15303711/windows-azure-messagelocklostexception – Aaron Newton Mar 04 '21 at 01:32

1 Answers1

3

The issue is unlikely with the sending code.

How do I update the below code to check if message is added to the topic or not.

You don't. When a message is successfully dispatched, it means the broker has received it, and it's guaranteed to be on the server. What you should do is verify that the message is not discarded or processed w/o noticing. The simplest way to verify that is to create a "catch-all subscription" under the topic that collects all messages and check if your messages were sent or not. I do not recommend this outside of your development/testing environment, as that's not something you should be doing in production.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
  • Thank you! Could you please provide a sample code to create catch-all subscription under the topic? – user989988 Mar 03 '21 at 21:44
  • @user989988 or you could just disable the subscribers. Then you'll see the messages pile up until you re enable them. – mason Mar 03 '21 at 22:16
  • Thank you! I disabled the subscription. What I noticed is that the message is not getting added to the topic. – user989988 Mar 03 '21 at 23:22
  • @user989988 if a message is not added to the topic and no exception is being thrown then it's either a topic setting or your sender code is off. Share a repro somewhere, please. Keep secrets out of that public repo. – Sean Feldman Mar 03 '21 at 23:52
  • Thank you! Here is the code in my repo: https://github.com/microsoftgraph/group-membership-management/blob/master/Service/GroupMembershipManagement/Hosts/JobTrigger/Services/SyncJobTopicsService.cs (Line 81) – user989988 Mar 04 '21 at 00:27
  • I see this exception in application insights: The lock supplied is invalid. Either the lock expired, or the message has already been removed from the queue. Not sure if this could be the issue. – user989988 Mar 04 '21 at 01:04
  • Lock is used when receiving and processing messages, not sending. – Sean Feldman Mar 04 '21 at 01:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/229490/discussion-between-sean-feldman-and-user989988). – Sean Feldman Mar 04 '21 at 05:18