static async Task SendMessagesAsync(int numberOfMessagesToSend) { try { for (var i = 0; i < numberOfMessagesToSend; i++) { // Create a new message to send to the topic. var sovMessage = new SOVMessage() { SOVId = 2345, FacilityNumber = 13, RiskScore = null, Longitude = null, Latitude = null, FacilityName = "Hospital A", TotalInsuredValue = 25000.45 }; string jsonMessage = JsonConvert.SerializeObject(sovMessage); var message = new Message(Encoding.UTF8.GetBytes(jsonMessage)); // Write the body of the message to the console. Console.WriteLine($"Sending message: {jsonMessage}"); // Send the message to the topic. await topicClient.SendAsync(message); } } catch (Exception exception) { Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}"); } }
static async Task ProcessMessagesAsync(Message message, CancellationToken token) { // Process the message. SOVMessage sovMessage = JsonConvert.DeserializeObject <SOVMessage>(Encoding.UTF8.GetString(message.Body)); Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} SOVId:{sovMessage.SOVId}; FacilityName: {sovMessage.FacilityName}"); // Complete the message so that it is not received again. // This can be done only if the subscriptionClient is created in ReceiveMode.PeekLock mode (which is the default). await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken); // Note: Use the cancellationToken passed as necessary to determine if the subscriptionClient has already been closed. // If subscriptionClient has already been closed, you can choose to not call CompleteAsync() or AbandonAsync() etc. // to avoid unnecessary exceptions. }