Search code examples
c#.net-coreazure-cosmosdb

Azure Cosmos db return null for continuation token


To be honest this is a code that is copied over from a tutorial.

I have tried and just not sure why I am getting null value for continuationToken when the MaxItemCount is set to 1 and with the database having 1576 documents.

 public async Task<VolcanoResult> GetVolcanoes()
        {
            VolcanoResult result = new VolcanoResult();
            var continuationToken = default(string);
            do
            {
                result = await QueryFetchNextPage(continuationToken);
                continuationToken = result.ContinuationToken;
            } while (continuationToken != null);


            return result;
        }

        private async Task<VolcanoResult> QueryFetchNextPage(string continuationToken)
        {
            List<Volcano> volcanoes = new List<Volcano>();

            var container = _client.GetContainer("Volcanoes", "Volcanoes");
            var containerThroughput = await container.ReadThroughputAsync();

            var sql = "SELECT * FROM c";
            var iterator = container.GetItemQueryIterator<Volcano>(sql, continuationToken, requestOptions: new QueryRequestOptions()

            {
                //PartitionKey = new PartitionKey(),
                MaxItemCount = 1
            });
            var page = await iterator.ReadNextAsync();
            var itemCount = 0;

            foreach (var document in page)
            {

                Volcano volcano = new Volcano();
                volcano.VolcanoName = document.VolcanoName;
                volcano.Country = document.Country;
                volcano.Region = document.Region;
                volcano.Type = document.Type;
                volcanoes.Add(volcano);
                ++itemCount;

            }
            VolcanoResult VolcanoResult = new VolcanoResult();
            VolcanoResult.Status = 200;
            VolcanoResult.RecordCount = itemCount;
            VolcanoResult.ContinuationToken = continuationToken;
            VolcanoResult.Volcanoes = volcanoes;

            return VolcanoResult;
        }

Solution

  • The reason is that you are not reading the next continuationToken from FeedResponse<T>, but always passing along the initial input value, which is default(string).

    Since it is not passed in by ref, then it is NOT changed to new one in: container.GetItemQueryIterator<Volcano>(sql, continuationToken, requestOptions: new QueryRequestOptions());

    You should read a new one instead from page:

    VolcanoResult.ContinuationToken = page.ContinuationToken;