天蓝色服务总线主题

时间:2020-04-02 17:14:02

标签: azure azureservicebus azure-servicebus-queues azure-servicebus-topics

Azure服务总线主题中如何检查特定记录。以我为例,多个客户端将记录输入到服务总线主题中,因此我需要验证现有记录。如果主题中没有记录,则输入记录。我需要验证SendMessageAsync方法中的记录 以下是我的代码

    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));
    }

1 个答案:

答案 0 :(得分:2)

请勿执行此操作。服务总线是一种消息传递服务,而不是数据存储。 主题专门用于多播和将发送者/发布者与接收者/订阅者解耦。

如果您需要重复检测邮件,请使用为此专门设计的feature。您可以利用重复数据删除功能来处理所需的任何内容,我在here中对此进行了描述。