Cosmos DB SDK ReadNextAsync() Throws Exception When Query Has Zero Results (UWP)

Dan Guthrie 1 Reputation point
2020-08-12T20:07:09.35+00:00

Hello,

I am using the latest cosmos db sdk (3.12.0 when posted) from a new UWP app. When I call resultSetIterator.ReadNextAsync() I am getting an exception IF the query produces no results. The exception is a CosmosException stating "Response status code does not indicate success: InternalServerError (500); ......; Reason: (Enumeration has not started. Call MoveNext.);

The code executes without exception IF the query has results. The code is taken directly from the examples in the docs.

Is an exception the expected behavior when a query returns zero results? I would have expected resultSetIterator.HasMoreResults to return false.

Thank you for any insight,

Dan

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,632 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. KalyanChanumolu-MSFT 8,331 Reputation points
    2020-08-13T05:25:20.947+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.