我对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次。将它们声明为全局变量会导致冲突,并在我的方法中分享它们的用法吗?