Windows Azure QueueClient推荐缓存?

时间:2013-12-09 13:19:42

标签: c# azure azure-queues

刚刚创建了一个新的辅助角色来处理来自队列的消息。此默认示例包括开头的以下代码:

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

有人可以详细说明该演示附带的评论吗?

1 个答案:

答案 0 :(得分:3)

每次都不要创建新实例。只创建一个实例,并使用它。

//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");
        }
    }
}