CloubBlockBlob AcquireLease提供两个租用到两个worker角色

时间:2013-02-20 16:42:45

标签: c# azure-storage-blobs

我正在开发一个应用程序,我不时会遇到错误,我必须在blob上同步几个worker角色。

以下是我的工作人员角色的初始代码

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

问题的截图: enter image description here

问题是,当我调用AcquireLease方法时,它有时会给我两个租约......任何人都知道如何解决这个问题......

CloudBlockBlob操作是原子的吗?

1 个答案:

答案 0 :(得分:2)

在与一位来自微软的人谈话之后,仿真器似乎与存储服务本身存在一些差异。

在Azure上运行相同的代码工作正常!

此答案适用于模拟器v1.8.0.0。