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