ReadNextAsync - existing without an error in .NET API

Scott Klein 5 Reputation points
2025-01-15T01:03:11.08+00:00

I am using the Azure Cosmos DB API in a C# console app just to play around with it. I am trying to query a Cosmos DB database. It looks like it is connecting to the Cosmos DB database, and iterator.HasMoreResults is true when I hit that line. However, it hits the line "FeedResponse<dymamic> response = await iterator.ReadNextAsync(), it just exists. No error, nothing. So I am wondering what I am doing wrong.

I have tried two different ways (you can see the var iterator commented out)...got the same result using that line. As far as the online documentation goes, it looks like the code is correct, so I am not sure why it is just existing on the ReadNextAsync line. Any help is greatly appreciated.

CosmosClient client = new CosmosClient(Endpointurl, PrimaryKey);

Database db = client.GetDatabase(DatabaseId);

Container container = db.GetContainer(ContainerId);

var query = new QueryDefinition("SELECT * FROM c ");

try

{

    //var iterator = container.GetItemQueryIterator<dynamic>(query);

    using FeedIterator<dynamic> iterator = container.GetItemQueryIterator<dynamic>( queryDefinition: query );

    while (iterator.HasMoreResults)

    {

        FeedResponse<dynamic> response = await iterator.ReadNextAsync();

        foreach (var item in response)

        {

            Console.WriteLine(item);

        }

    }

}

catch (Exception ex)

{

    Console.WriteLine(ex);

}

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,738 questions
{count} votes

1 answer

Sort by: Most helpful
  1. NIKHILA NETHIKUNTA 4,425 Reputation points Microsoft Vendor
    2025-01-15T15:37:38.59+00:00

    @Scott Klein
    Thank you for the question and for using Q&A platform.

    Have you checked to make sure that your connection string is correct? Also, have you verified that your query is returning data? You can try running the query in the Azure portal to see if it returns any results. Ensure your container has items in it. If the container is empty, HasMoreResults could still be true on the first iteration, but the result set would be empty.

    You can also refer to the below links for more help regarding the code:
    https://medium.com/@emer.kurbegovic/working-with-azure-cosmos-db-in-net-core-a-step-by-step-guide-c87411b610cb
    https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/how-to-dotnet-query-items

    Hope this helps. Please let us know if you have any further queries.

    0 comments No comments

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.