Windows Azure QueueClient recommended cache?

500 views Asked by At

Just created a new worker role to process messages coming from queue. The default example for this include the following code at the beginning:

// QueueClient is thread-safe. Recommended that you cache 
// rather than recreating it on every request
QueueClient Client;

Can anyone elaborate on the comment that comes with that demo?

1

There are 1 answers

1
Thiago Custodio On BEST ANSWER

don't create new instances every time. Create just one instance, and use it.

//don't this
public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {
        // This is a sample worker implementation. Replace with your logic.
        Trace.TraceInformation("WorkerRole1 entry point called", "Information");

        while (true)
        {
            QueueClient Client = new QueueClient();
            Thread.Sleep(10000);
            Trace.TraceInformation("Working", "Information");
        }
    }
}

//use this
public class WorkerRole : RoleEntryPoint
{
    public override void Run()
    {
        // This is a sample worker implementation. Replace with your logic.
        Trace.TraceInformation("WorkerRole1 entry point called", "Information");

        QueueClient client = new QueueClient();

        while (true)
        {
            //client....
            Thread.Sleep(10000);
            Trace.TraceInformation("Working", "Information");
        }
    }
}