Search code examples
c#azurepaginationgetazure-cosmosdb

Paging in DocumentDB with Web application


This is the code that I am trying to implement paging for DocumentDB to get all the students in 10th class A section.

It is working fine in console application.

But when I am trying to do in my web application, at async call (i.e; query.ExecuteNextAsync()) the process is terminating and It even doesn't give any exception.

public List<Student> allStudents()
        {
            int class = 10;
            string section = "A";
            string documentType = "Student";
            List<Student> students = new List<Student>();
            string DocumentDBCollectionLink = "CollectionLink";
            DocumentClient dc = new DocumentClient(new Uri("https://xyz.documents.azure.com:443/"), "authenticationKey==");
            IDocumentQuery<Student> query;
            String continuation = "";
            do
            {
                FeedOptions feedOptions = new FeedOptions { MaxItemCount = 10, RequestContinuation = continuation };
                query = dc.CreateDocumentQuery<Student>(DocumentDBCollectionLink, feedOptions).Where(d => d.Type.Equals(documentType) && d.Class.Equals(class) && d.Section.Equals(section)).AsDocumentQuery();

                FeedResponse<Student> pagedResults = this.getRecords(query).Result;
                foreach (Student system in pagedResults)
                {
                    students.Add(system);
                }
                continuation = pagedResults.ResponseContinuation;
            } while (!String.IsNullOrEmpty(continuation));
            return students;
        }
    private async Task<FeedResponse<Student>> getRecords(IDocumentQuery<Student> query)
    {
        FeedResponse<Student> pagedResults = await query.ExecuteNextAsync<Student>();
        return pagedResults;
    }

I am not getting why it is executing in console application, but not in web application.

Anything wrong with this?

Or any better way to get the result?

Any help would be greatly appreciated.

Thanks in advance.


Solution

  • try using

    FeedResponse pagedResults = query.ExecuteNextAsync().Result;

    check this