0

How do i to check specific record exist in azure service bus topic. In my case, multiple client enter record into service bus topic so i need to validate the existing record. If not record exist in topic than enter the record. i need to validate record in SendMessageAsync method following is my code

    private ITopicClient client;
    private DateTime tokenExpiresAtUtc = DateTime.MinValue;
    private string tokenValue = string.Empty;
    public async Task SendOrderMessageAsync(string OrderNumber, int RecipientStoreId)
    {
        await SendMessageAsync(GetMessage(JsonConvert.SerializeObject(new OrderNotificationViewModel
        {
            SenderStoreId = this.SenderStoreId.Value,
            NotificationType = NotificationTypes.Order,
            OrderNumber = OrderNumber,
            StoreId = RecipientStoreId
        })), RecipientStoreId);

    }
    private async Task SendMessageAsync(Message message, int StoreId)
    {
        try
        {
            await (await GetClient(GetTopicName(StoreId))).SendAsync(message);
        }
        catch (MessagingEntityNotFoundException ex)
        {
            var topic = await CreateTopic(GetTopicName(StoreId));
            var subscription = await CreateSubscription(topic.Name, GetSubscriptionName(StoreId));
            await (await GetClient(GetTopicName(StoreId))).SendAsync(message);
        }
    }
    public async Task SendOrderNotesMessageAsync(string OrderNumber, List<string> Notes, int RecipientStoreId)
    {
        await SendMessageAsync(GetMessage(JsonConvert.SerializeObject(new OrderNotesNotificationViewModel
        {
            SenderStoreId = this.SenderStoreId.Value,
            NotificationType = NotificationTypes.OrderNotes,
            OrderNumber = OrderNumber,
            Notes = Notes,
            StoreId = RecipientStoreId
        })), RecipientStoreId);
    }
    private async Task<ITopicClient> GetClient(string TopicName)
    {
        if (client != null && client.TopicName != TopicName)
        {
            await client.CloseAsync();
            client = null;
        }
        if (client == null)
        {
            client = new TopicClient(GlobalConfig.Instance.ServiceBusConnectionString, TopicName);
        }
        return client;
    }
    private string GetTopicName(int StoreId)
    {
        return GlobalConfig.Instance.ServiceBusTopicNamePattern.Replace("{StoreId}", StoreId.ToString());
    }
    private string GetSubscriptionName(int StoreId)
    {
        return GlobalConfig.Instance.ServiceBusSubscriptionNamePattern.Replace("{StoreId}", StoreId.ToString());
    }
    private Message GetMessage(string messageBody)
    {
        return new Message(Encoding.UTF8.GetBytes(messageBody));
    }
Mashhad Saleem
  • 176
  • 1
  • 2
  • 17

1 Answers1

2

Do not do this. Service Bus is a messaging service, not a data store. Topics specifically are designed for multi-casting and decoupling senders/publishers from the receivers/subscribers.

If you need duplicate message detection, use a feature specifically designed for that. You can leverage deduplication to work with any content you need, which I described here.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80
  • i have exactly same thought but in my case there are multiple publisher so it must be a validation before publish any message. I am starting deployment of your solution and let you know . Thanks for your kind support – Mashhad Saleem Apr 03 '20 at 09:34