2

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.

5 Answers 5

2

try using

FeedResponse pagedResults = query.ExecuteNextAsync().Result;

check this

2
FeedResponse pagedResults = query.ExecuteNextAsync().Result;

This line called an async method in blocking way, and may cause deadlock in asp.net environment. see: http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

2

All async methods should be ConfigureAwait(false) in web calling

query.ExecuteNextAsync().ConfigureAwait(false)
1

FeedResponse pagedResults = query.ExecuteNextAsync().Result;

its working fine for me, but I need the reason why it is not working

0

do not use ConfigureAwait(false) because you will not get the results back. Also, do no mix async code with sync code. It is a receipt for disaster. Even if some people say that .Result -works-, believe me, it does not in a server environment running in an async context. The -why- it does not work, is a long story. I'll not try to explain for now :)

So go for this:

 public async List<Student> allStudentsAsync()
        {

         result = new List<Student>();
        while (query.HasMoreResults)
        {
            var response = await query.ExecuteNextAsync<T>();
                         result.AddRange(response);
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.