简体   繁体   English

带有ASP.NET Web窗体的Azure CosmosDB SDK v3

[英]Azure CosmosDB SDK v3 with ASP.NET Web Forms

I have an issue with the following code, working well in a Console App project and not working in a ASP.NET Web Forms project, both targeting .NET Framework 4.7.2. 我的以下代码存在问题,它们都在Console App项目中正常运行,而在ASP.NET Web Forms项目中均不工作,两者均以.NET Framework 4.7.2为目标。

The goal is to use the last Azure Cosmos DB SDK (v3) to get all documents in a container, without specifying a type (use of dynamic). 目标是使用最新的Azure Cosmos DB SDK(v3)来获取容器中的所有文档,而无需指定类型(使用动态)。

I've tried to target both the emulator (the last version 2.4.5) and the Azure Cosmos service. 我尝试同时针对模拟器(最新版本2.4.5)和Azure Cosmos服务进行定位。

In the ASP.NET project, the execution of queryResultSetIterator.ReadNextAsync().Result never ends (no timeout). 在ASP.NET项目中,queryResultSetIterator.ReadNextAsync()。Result的执行永远不会结束(没有超时)。

string endpointUri = "https://localhost:8081";
string primaryKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
string database = "SendGridEvents";
string collection = "SendGridEvents";
using (CosmosClient client = new CosmosClient(endpointUri, primaryKey))
{
    Container container = client.GetDatabase(database).GetContainer(collection);

    QueryDefinition queryDefinition = new QueryDefinition("SELECT * FROM c");
    FeedIterator<dynamic> queryResultSetIterator = container.GetItemQueryIterator<dynamic>(queryDefinition);
    List<dynamic> documents = new List<dynamic>();
    while (queryResultSetIterator.HasMoreResults)
    {
        FeedResponse<dynamic> currentResultSet = queryResultSetIterator.ReadNextAsync().Result;
        foreach (var document in currentResultSet)
        {
            documents.Add(document);
        }
    }
}

This is caused due to a deadlock because of the use of the problematic .Result . 这是由于使用有问题的.Result导致死锁而引起的。

There are countless of sources that explain this deadlock but I will link this answer from StackOverflow. 有无数来源解释此死锁,但我将从StackOverflow链接此答案

The TL;DR is that you are blocking an otherwise asynchronous call in the UI thread which is causing the deadlock. TL; DR表示您正在阻止UI线程中否则会导致死锁的异步调用。 You need to properly await your call like this: 您需要像这样正确等待电话:

FeedResponse<dynamic> currentResultSet = await queryResultSetIterator.ReadNextAsync();

You could technically block the call if you used the ConfigureAwait(false) approach but the v3 SDK does not cascade this call all the way to the network calls so it wouldn't make any difference. 如果您使用ConfigureAwait(false)方法,但是从技术上讲,您可以阻止该调用,但是v3 SDK不会将此调用一直级联到网络调用,因此不会有任何区别。 WebForms allows you to have async handlers so you would be fine to make your method async and try again. WebForms允许您具有异步处理程序,因此可以使方法异步并重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM