The below program sends the message to the queue, however most of the times the message is not sent.
I am using the Sender() method (as shown below) in my class library. This is being called from a Cloud Service(Worker Role). Please help to identify why the message is not consistently sent and correcting the code.
{
static void Main(string[] args)
{
Sender();
Console.ReadKey();
}
private static void Sender()
{
var ConnectionString = "<<Connectionstring>>";
var QueueName = "samplequeue";
var queueClient = new QueueClient(ConnectionString, QueueName);
try
{
for (var i = 0; i < 5; i++)
{
// Create a new message to send to the queue
string messageBody = $"Message {i}";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
// Write the body of the message to the console
Console.WriteLine($"Sending message: {messageBody}");
// Send the message to the queue
queueClient.SendAsync(message);
}
}
catch (Exception ex)
{
Console.WriteLine("Message queue failed\n" + ex.Message);
}
finally
{
Console.WriteLine("Message queued");
}
}
}