0

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");
        }
    }
}
Swinkaran
  • 1,207
  • 1
  • 12
  • 19
  • have you try to fix the code first and await the sending command? It was pointed out in the [GitHub issue](https://github.com/Azure/azure-service-bus/issues/331#issuecomment-579935655) you've raised earlier. – Sean Feldman Jan 30 '20 at 04:20

2 Answers2

1

I believe the problem is that you're not awaiting the execution of an async method.

queueClient.SendAsync(message)

One option would be to change the above call and wait for the execution to complete

await queueClient.SendAsync(message);

In this case you will need to make Sender method async as well and then wait for it to complete in your Main method.

Other option would be to use Sync version of the method:

queueClient.Send(message); 
Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
  • Thanks Gaurav for the response. if i add await/async, the whole caller methods have to be async, which i don't have control, as the Sender method will be called by other applications also. and i don't see Send method in the Microsoft.Azure.ServiceBus.QueueClient class. can you point me to the right class? – Anjan Kumar Jan 30 '20 at 04:49
  • `if i add await/async, the whole caller methods have to be async, which i don't have control, as the Sender method will be called by other applications also` - That's correct. – Gaurav Mantri Jan 30 '20 at 04:58
  • `and i don't see Send method in the Microsoft.Azure.ServiceBus.QueueClient class. can you point me to the right class?` - I looked here: https://learn.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.queueclient.send?view=azure-dotnet#Microsoft_ServiceBus_Messaging_QueueClient_Send_Microsoft_ServiceBus_Messaging_BrokeredMessage_ which I believe this is an older SDK. Can you tell me which SDK you're using?` – Gaurav Mantri Jan 30 '20 at 04:59
0

You must wait on the call to complete. Otherwise it will be run in the background.

That means that Message 4 might complete after Message 3 (since you have a loop) or that it wont complete at all if your console application exits before it have a chance.

Since you are using a synchronous application, you can invkoe Send() like this:

queueClient.SendAsync(message).GetAwaiter().GetResult();

See .Result vs .GetAwaiter().GetResult()

jgauffin
  • 99,844
  • 45
  • 235
  • 372