0
votes

I am using service bus to communicate between web role and worker role, but it has been giving me some erratic results. I have deployed my cloud project to azure. So surprisingly when i run just the deployed project, the service bus works fine but when i also run the same project on my local emulator which is also using the service bus, service bus becomes faulty as in all the messages either to go to dead letter or are not received by the worker role. Is this a normal behavior for service bus? If not what could be the problem and how can i fix it?

This is irritating for me because now the service bus never works fine when i run my project on my local emulator.

This is how am sending and receiving messages, but i guess the problem might not be in it because it works fine when the service bus is being used just by the deployed project.

Worker Role:

public override void Run()
{
    while (!IsStopped)
    {
        try
        {
            if (BroadcastReceived)
            {
                BroadcastReceived = false;
                // Receive the message from Web Role to upload the broadcast to queue
                BroadcastClient.BeginReceive(OnWebRoleMessageReceived, null);
            }

            if (SignalRMessageReceived)
            {
                SignalRMessageReceived = false;
                // Receive the message from SignalR BroadcastHub
                SignalRClient.BeginReceive(OnSignalRMessageReceived, null);
            }

            if (SignalRFirstTimeMessageReceived)
            {
                SignalRFirstTimeMessageReceived = false;
                // Receive the message from SignalR BroadcastHub
                SignalRFirstTimeClient.BeginReceive(OnSignalRFirstTimeMessageReceived, null);
            }          
     }
 }


public void OnWebRoleMessageReceived(IAsyncResult iar)
{
    BrokeredMessage receivedBroadcastMessage = null;
    receivedBroadcastMessage = BroadcastClient.EndReceive(iar);

    if (receivedBroadcastMessage != null)
    {
        // Process the message
       receivedBroadcastMessage.Complete();
    }
BroadcastReceived = true;
 }

Web Role:

var queueClient = CloudStorageHelper.GetServiceBusQueueClient(Queuenames.ApiToWorkerRole);            

        BrokeredMessage message = new BrokeredMessage(record);

        queueClient.Send(message);

How am creating the service bus queues:

public static QueueClient GetServiceBusQueueClient(string queuename)
{
    string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");

    var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);

    if (!namespaceManager.QueueExists(queuename))
    {
        namespaceManager.CreateQueue(queuename);
    }

    QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, queuename);



    return Client;
}
2
You might want to read/write the variables shared across threads in a thread-safe way. (e.g. Volatile.Read and Volatile.Write). You could either be missing values due to caching or due to run-time compiler optimizations.Peter Ritchie
Can you please elaborate on that with some code snippet in my context? or any link?Bitsian

2 Answers

0
votes

Can you please elaborate on the following?

1) Are you using the same Service Bus connection strings for debugging and the deployed service? Do note that Service Bus Queues/Topics do not run in the emulator, even when your cloud service is being debugged there so you are always hitting the live service in Azure.

2) Check the MaxDeliveryCount and TimeToLive properties on the Queue/Topic/Messages. Messages are dead lettered when DeliveryCount on a message exceeds MaxDeliveryCount, this is incremented each time you receive and do NOT complete the message (you may want to set a higher value when debugging)

0
votes

The issue was arising because in our worker role we continuously check the service bus queues to see if a message has arrived. And when we start both the local and cloud worker roles, then both the worker roles try to receive the new message from the same service bus queue. And hence sometimes the cloud one accepts it and sometimes the local one.

Using different service bus queue names for cloud and local projects resolved the issue.