When using a Cosmos DB FeedIterator, how can I ignore serialization failures and continue reading the rest of the valid documents?
The Cosmos DB method FeedIterator.ReadNextAsync() can return multiple documents at a time. In case of a serialization error in one of the documents, the rest of the valid documents will be dropped (a JsonSerializationException exception is thrown).
SDK version: SQL API for .NET via the NuGet package Microsoft.Azure.Cosmos version 3.28.0.
public async Task<ICollection<T>> FetchAll(CancellationToken cancellationToken)
{
var items = new List<T>();
using var iterator = this.container.GetItemQueryIterator<T>();
while (iterator.HasMoreResults)
{
try
{
foreach (var item in await iterator.ReadNextAsync(cancellationToken).ConfigureAwait(false))
{
items.Add(item);
}
}
catch (JsonSerializationException ex)
{
// When getting to here - the entire bulk has failed (100 documents) even if only a single document is invalid.
}
}
return items;
}
this.container is a CosmosClient with default cosmosClientOptions.