【问题标题】:Paging in DocumentDB with Web application使用 Web 应用程序在 DocumentDB 中分页
【发布时间】:2015-03-11 09:45:27
【问题描述】:

这是我尝试为 DocumentDB 实现分页以让所有学生进入第 10 级 A 部分的代码。

它在控制台应用程序中运行良好。

但是当我尝试在我的 Web 应用程序中执行操作时,在异步调用(即 query.ExecuteNextAsync())时,该进程正在终止,它甚至没有给出任何异常。

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;
    }

我不明白为什么它在控制台应用程序中执行,而不是在 Web 应用程序中。

这有什么问题吗?

或者有什么更好的方法来获得结果?

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

    标签: c# azure get paging azure-cosmosdb


    【解决方案1】:

    尝试使用

    FeedResponse pagedResults = query.ExecuteNextAsync().Result;

    检查一下

    【讨论】:

      【解决方案2】:
      FeedResponse pagedResults = query.ExecuteNextAsync().Result;
      

      该行以阻塞方式调用了异步方法,在asp.net环境中可能会导致死锁。 见:http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

      【讨论】:

        【解决方案3】:

        Web 调用中的所有异步方法都应为 ConfigureAwait(false)

        query.ExecuteNextAsync().ConfigureAwait(false)
        

        【讨论】:

          【解决方案4】:

          FeedResponse pagedResults = query.ExecuteNextAsync().Result;

          它对我来说工作正常,但我需要它不工作的原因

          【讨论】:

            【解决方案5】:

            不要使用 ConfigureAwait(false),因为你不会得到结果。 此外,不要将异步代码与同步代码混合。这是灾难的收据。即使有人说 .Result -works-,相信我,它也不会在异步上下文中运行的服务器环境中。 -为什么-它不起作用,说来话长。我暂时不解释了:)

            那就去吧:

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2010-10-21
              • 2012-02-26
              • 1970-01-01
              • 2010-10-20
              • 2013-04-07
              相关资源
              最近更新 更多