我正在开发一个应用程序,但有时会出现错误,我必须将多个工作角色与 blob 上的租约同步。
下面是我的工人角色的初始化代码
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageAccount"));
string blobContainerName = RoleEnvironment.GetConfigurationSettingValue("BlobContainer");
CloudBlobContainer blobContainer = storageAccount.CreateCloudBlobClient().GetContainerReference(blobContainerName);
blobContainer.CreateIfNotExists();
string blobName = RoleEnvironment.GetConfigurationSettingValue("BlobToBeLeased");
_blob = blobContainer.GetBlockBlobReference(blobName);
if (!_blob.Exists())
{
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes("This is dummy data")))
{
try
{
_blob.UploadFromStream(ms);
}
catch (StorageException storageException)
{
if (storageException.RequestInformation.HttpStatusCode != 412)
throw;
}
}
}
这是我的 AcquireLease 方法:
private void AcquireLease()
{
try
{
var leaseId = _blob.AcquireLease(null, null);
Trace.WriteLine("==========> Lease acquired! <========== ID => " + leaseId);
_accessCondition = new AccessCondition {LeaseId = leaseId};
}
catch (Exception)
{
Trace.WriteLine("==========> Lease rejected! <==========");
}
}
问题截图:
问题是当我调用 AcquireLease 方法时,它有时会给我两个租约......任何人都知道如何解决这个问题......
CloudBlockBlob 操作是原子的吗?