Azure表存储错误:"操作的意外响应代码:99"

sel*_*ary 7 exception azure azure-table-storage

执行以下代码时出现此错误:

var insert = new TableBatchOperation();
foreach (var entity in entities)
{
    insert.Insert(entity);
}
cloudTable.ExecuteBatch(insert);  
Run Code Online (Sandbox Code Playgroud)

实体集合包含512个元素.Azure SDK通过StorageException:

"Unexpected response code for operation : 99" 
Run Code Online (Sandbox Code Playgroud)

这个错误意味着什么,我该如何解决?

sel*_*ary 18

这种无描述性的错误意味着Azure批量操作(至少在这种情况下)最多需要100个元素.限制你的批次,你会很好.

我最终使用了这样的东西:

public void Insert(IEnumerable<T> entities)
{
    foreach (var chunk in entities.Chunk(100))
    {
        InsertMaxLimitElements(chunk);
    }
}

private void InsertMaxLimitElements(IEnumerable<T> chunk)
{
    var insert = new TableBatchOperation();

    foreach (var entity in chunk)
    {
        insert.Insert(entity);
    }
    cloudTable.ExecuteBatch(insert);
}
Run Code Online (Sandbox Code Playgroud)

从这个答案中复制了Chunk扩展方法:

public static IEnumerable<IEnumerable<T>> Chunk<T>(this IEnumerable<T> source, int chunksize)
{
    while (source.Any())
    {
        yield return source.Take(chunksize);
        source = source.Skip(chunksize);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我想在此处包含的其他一些事项 - 1)事务中的所有实体应该具有相同的PartitionKey 2)事务中的实体只能出现一次3)即使实体的最大大小可以是1 MB,也是最大值批量大小可以是4 MB.有关这方面的更多信息,请访问:http://msdn.microsoft.com/en-us/library/windowsazure/dd894038.aspx (4认同)