private void SendMessages() { try { if (cancellationToken.IsCancellationRequested) { return; } byte[] body = null; Dictionary <string, object> headers = null; if (!string.IsNullOrWhiteSpace(Template)) { try { var rawMessage = MessageTestHelpers.GetMessageFromFile(Template); var data = rawMessage.GetEelementsForRabbitPublish(); body = data.Item1; headers = data.Item2; Console.WriteLine($"Sending message from {Template}"); } catch (Exception err) { Console.WriteLine($"Could not get message from {Template}. The error is {err}"); throw; } } if (string.IsNullOrWhiteSpace(Template) || (body == null && headers == null)) { body = Encoding.UTF8.GetBytes("Hello World!"); headers = new Dictionary <string, object> { { "one", Encoding.UTF8.GetBytes("Header value") } }; Console.WriteLine("Sending HelloWorld!"); } var factory = new ConnectionFactory() { Uri = connectionString }; using (var connection = factory.CreateConnection()) { long messageCount = 0; while (!cancellationToken.IsCancellationRequested && (MaxMessages == 0 || messageCount < MaxMessages)) { using (var channel = connection.CreateModel()) { channel.BasicPublish(Exchange, "", new BasicProperties { Headers = headers }, body); messageCount++; Console.Write("."); } Thread.Sleep(60000 / MessagesPerMinute); consecutiveRetryCount = 0; } Console.WriteLine("Done"); } } catch (Exception err) { Console.WriteLine($"Failed with error {err}"); throw; } }