我对 CloudStorageAccount 变量有疑问。我一直在我的代码中经常使用和声明它,我想知道将它声明为全局变量并在每个函数中使用它是否会导致冲突。我将用一个简单的例子来证明我的问题。
public class AzureQueueSvc : IQueueSvc
{
DoclQueue _queue;
public AzureQueueSvc(DoclQueue queue)
{
_queue = queue;
_queue.Connectionstring = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];
}
public CloudQueue GetQueue { get; }
public void CreateQueue()
{
CloudStorageAccount account = CloudStorageAccount.Parse(_queue.Connectionstring);
CloudQueueClient queueClient = account.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference(_queue.Name);
queue.CreateIfNotExists();
}
public bool QueueExists()
{
CloudStorageAccount account = CloudStorageAccount.Parse(_queue.Connectionstring);
CloudQueueClient queueClient = account.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference(_queue.Name);
if (queue.Exists())
return true;
else
return false;
}
public void SendMessage(DoclMessage message)
{
CloudStorageAccount account = CloudStorageAccount.Parse(_queue.Connectionstring);
CloudQueueClient queueClient = account.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference(_queue.Name);
queue.CreateIfNotExists();
CloudQueueMessage msg = new CloudQueueMessage(string.Format(message.ToString()));
queue.AddMessage(msg);
}
}
如您所见,我必须声明 CloudStorageAccount、CloudQueueClient 和 CloudQueue 3 次。将它们声明为全局变量并在我的方法中共享它们的用法会导致冲突吗?