7

I got this error when I executed the following code:

var insert = new TableBatchOperation();
foreach (var entity in entities)
{
    insert.Insert(entity);
}
cloudTable.ExecuteBatch(insert);  

Where the entities collection contained 512 elements. The Azure SDK through a StorageException:

"Unexpected response code for operation : 99" 

What does this error mean, and how can I solve this?

jeb
  • 78,592
  • 17
  • 171
  • 225
seldary
  • 6,186
  • 4
  • 40
  • 55

2 Answers2

18

This un-descriptive error means that Azure bulk operations (at least in this case) takes up to 100 elements. Limit your batch and you'll be good.

I ended up using something like this:

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);
}

The Chunk extension method was copied from this answer:

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);
    }
}
Community
  • 1
  • 1
seldary
  • 6,186
  • 4
  • 40
  • 55
  • 4
    A few other things I would like to include here - 1) All entities in a transaction should have same PartitionKey 2) An entity in a transaction can only appear once 3) Even though the maximum size of an entity can be 1 MB, the maximum size of a batch can be 4 MB. More information about this can be found here: http://msdn.microsoft.com/en-us/library/windowsazure/dd894038.aspx – Gaurav Mantri Aug 11 '13 at 10:33
0

If the batch size is < 100, it could also mean that you have duplicate entries (two entries with the same rowkey). This is very likely if it succeeds when you write the entries individually but fails writing in batch.

Mike S
  • 3,058
  • 1
  • 22
  • 12