Azure CloudTable.ExecuteBatch(TableBatchOperation)抛出storageexception.如何找到导致异常的操作?

Dav*_* S. 4 c# azure azure-storage

我有这个代码删除一些项目:

    private static void DeleteBatch(IList<TableEntity> toDelete)
    {
        if(toDelete == null)
            throw new ArgumentNullException("toDelete");
        if(toDelete.Count == 0)
            throw new ArgumentException("There is no elements in toDelete.");
        if(toDelete.GroupBy(e => e.PartitionKey).Count() > 1)
            throw new ArgumentException("The entities to delete must have the same PartitionKey.");

        Parallel.ForEach(Partitioner.Create(0, toDelete.Count, 100),
                         range =>
                         {
                             TableBatchOperation batchOperation = new TableBatchOperation();
                             for (Int32 i = range.Item1; i < range.Item2; i++)
                                 batchOperation.Delete(toDelete[i]);
                             _table.ExecuteBatch(batchOperation);
                         });
    }
Run Code Online (Sandbox Code Playgroud)

表实体以*ETag传递.

有时这会抛出一个StorageException: The specified resource does not exist.我认为这是一个404 HttpStatusCode.在这种情况下,我不关心它是否不存在,所以我想忽略导致它们的操作的这个异常.如何在批处理中忽略404 for invididual TableOperations,或者至少重试未抛出此异常的TableOperations的批处理操作(如何知道哪些操作失败).为了能够找到导致404的操作,单独进行每个操作感觉非常无效.

Gau*_*tri 9

在批处理操作中,我认为不可能忽略错误.您可以做一件事来确定批处理中的哪个实体失败,并且可以通过捕获StorageException并检查RequestInformation.ExtendedErrorInformation属性来完成.请查看下面的屏幕截图,特别是ErrorCode和ErrorMessage.我在这里所做的是在我的批处理中第二个实体在删除实体批处理操作中失败.您将获得ErrorCode作为"ResourceNotFound",但有趣的是ErrorMessage.如果你看到,你得到ErrorMessage为"1:指定的资源不存在".它基本上为您提供批次中失败的实体的索引.

在此输入图像描述

你可以做的是将批处理拆分为3个部分 - 在这个失败的实体之前,这个失败的实体(单个项目)然后在这个失败的实体之后的实体并在单独的操作中尝试它们.