Hello @Dan Guthrie Welcome to the Microsoft Q&A forums and thank you for your interest in Azure Cosmos DB
I believe you are using container.GetItemQueryIterator() to get results and using resultSetIterator.ReadNextAsync() to loop through them
Please switch to the stream iterators introduced in v3 Api for better performance. These do not throw exceptions.
You can change your code to use GetItemQueryStreamIterator. Here is a code sample
QueryDefinition query = new QueryDefinition(
"select * from sales s where s.AccountNumber = @AccountInput ")
.WithParameter("@AccountInput", "Account1");
// Get the cosmos response message to access the stream directly
FeedIterator streamResultSet = container.GetItemQueryStreamIterator(
query,
requestOptions: new QueryRequestOptions()
{
PartitionKey = new PartitionKey("Account1"),
MaxItemCount = 10,
MaxConcurrency = 1
});
List<SalesOrder> allSalesForAccount1FromStream = new List<SalesOrder>();
while (streamResultSet.HasMoreResults)
{
using (ResponseMessage responseMessage = await streamResultSet.ReadNextAsync())
{
// Item stream operations do not throw exceptions for better performance
if (responseMessage.IsSuccessStatusCode)
{
dynamic streamResponse = FromStream<dynamic>(responseMessage.Content);
List<SalesOrder> salesOrders = streamResponse.Documents.ToObject<List<SalesOrder>>();
Console.WriteLine($"\n1.4.3 - Item Query via stream {salesOrders.Count}");
allSalesForAccount1FromStream.AddRange(salesOrders);
}
else
{
Console.WriteLine($"Query item from stream failed. Status code: {responseMessage.StatusCode} Message: {responseMessage.ErrorMessage}");
}
}
}
Do let me know if you face any issues.
We have all these samples and best practices on our GitHub repo if you want to have a look.
----------
If an answer is helpful, please "Accept answer" or "Up-Vote" for the same which might be beneficial to other community members reading this thread.