2021年3月3日星期三

Verify if message is added to the service bus topic

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

public class Service  {      private readonly IServiceBusTopicsRepository _serviceBusTopicsRepository;            public Service(IServiceBusTopicsRepository serviceBusTopicsRepository)      {          _serviceBusTopicsRepository = serviceBusTopicsRepository;      }        public async Task ProcessAsync()      {             var runningJobs = new List<Job>();          // var jobs = <get-jobs>            await foreach (var job in jobs)          {              job.Status = true;              await _serviceBusTopicsRepository.AddMessageAsync(job);          }             }  }              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.UserProperties.Add("Type", job.Type);          message.MessageId = $"{job.PartitionKey}_{job.RowKey}";            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.

UPDATE:

I can see that after executing the below code, the status of each job is set to true all the time. However, message is not getting added to the topic all the time. What could be the issue?

await foreach (var job in jobs)  {      job.Status = true;      await _serviceBusTopicsRepository.AddMessageAsync(job);  }     

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.

https://stackoverflow.com/questions/66464375/verify-if-message-is-added-to-the-service-bus-topic March 04, 2021 at 04:23AM

没有评论:

发表评论