调用 ReplaceDocumentAsync 方法失败 return
Call to ReplaceDocumentAsync method fails to return
在 Web 上下文 (IIS) 中 运行 时,我没有从异步 DocumentDB ReplaceDocument 方法获得响应。当 运行 在本地时,它工作得很好。根据我的研究,这似乎与 UI 线程的冲突死锁有关?
我从其他异步调用中得到了很好的响应,例如:
this.Client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(this.DatabaseName)).Result;
// and
this.Client.CreateDatabaseAsync(new Database { Id = this.DatabaseName }).Result;
// etc
我没有使用 await,因为在 IIS 上下文中似乎从不响应。我不确定为什么,所以我删除了所有异步等待,并开始使用 .Result
,现在除了下面的方法之外一切正常。
根据这个问题Call to await GetFileAsync() never returns and app hangs in WinRT app。我已经设置了 ConfigureAwait(false)
并且正在调用 GetResult()
。但是该方法没有 return,并且线程最终在没有 return.
的情况下关闭
var task = this.Client.ReplaceDocumentAsync(this.GetDocumentLink(d.id), d, options);
var configuredTask = task.ConfigureAwait(false);
var awaiter = configuredTask.GetAwaiter();
var result = awaiter.GetResult();
我也试过以下排列组合:
// in a spereate async method
await this.Client.ReplaceDocumentAsync(this.GetDocumentLink(d.id), d, options);
await this.Client.ReplaceDocumentAsync(this.GetDocumentLink(d.id), d, options).ConfigureAwait(false);
// as well as trying a call back
.GetAwaiter().OnCompleted(() => /* never gets here either */);
编辑
我的 Web.config
中有这个(来自 :What's the meaning of "UseTaskFriendlySynchronizationContext"?)
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
and
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.5" />
</system.web>
我相信您看到的是我在博客上描述的 common deadlock issue。
最好的解决办法是不要尽量避免到处使用ConfigureAwait(false)
,因为必须到处 ].所以如果你忘记了一个地方,或者如果有一些库代码没有使用它(遗憾的是很常见),那么你就无法避免死锁。
最好的解决办法是拥抱使用await
。将对 Task.Wait()
、Task<T>.Result
和 Task.GetAwaiter().GetResult()
的每次调用替换为 await
:
await this.Client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(this.DatabaseName));
await this.Client.CreateDatabaseAsync(new Database { Id = this.DatabaseName });
var result = await this.Client.ReplaceDocumentAsync(this.GetDocumentLink(d.id), d, options);
I'm not using await as in an IIS context is seems to never respond.
请确保您的目标是 .NET 4.5 并且 已在 web.config
.
中将 targetFramework
设置为 4.5
在我的例子中,有类似的任务 cancel/error、显示的内部异常 "DictionaryError" 或聚合异常。至少我发现导致问题的主要原因只是通过 http:// 使用端点到 CosmosDb 模拟器。更改后 https://localhost:8081 - 一切开始工作。
在 Web 上下文 (IIS) 中 运行 时,我没有从异步 DocumentDB ReplaceDocument 方法获得响应。当 运行 在本地时,它工作得很好。根据我的研究,这似乎与 UI 线程的冲突死锁有关?
我从其他异步调用中得到了很好的响应,例如:
this.Client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(this.DatabaseName)).Result;
// and
this.Client.CreateDatabaseAsync(new Database { Id = this.DatabaseName }).Result;
// etc
我没有使用 await,因为在 IIS 上下文中似乎从不响应。我不确定为什么,所以我删除了所有异步等待,并开始使用 .Result
,现在除了下面的方法之外一切正常。
根据这个问题Call to await GetFileAsync() never returns and app hangs in WinRT app。我已经设置了 ConfigureAwait(false)
并且正在调用 GetResult()
。但是该方法没有 return,并且线程最终在没有 return.
var task = this.Client.ReplaceDocumentAsync(this.GetDocumentLink(d.id), d, options);
var configuredTask = task.ConfigureAwait(false);
var awaiter = configuredTask.GetAwaiter();
var result = awaiter.GetResult();
我也试过以下排列组合:
// in a spereate async method
await this.Client.ReplaceDocumentAsync(this.GetDocumentLink(d.id), d, options);
await this.Client.ReplaceDocumentAsync(this.GetDocumentLink(d.id), d, options).ConfigureAwait(false);
// as well as trying a call back
.GetAwaiter().OnCompleted(() => /* never gets here either */);
编辑
我的 Web.config
中有这个(来自 :What's the meaning of "UseTaskFriendlySynchronizationContext"?)
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
and
<system.web>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.5" />
</system.web>
我相信您看到的是我在博客上描述的 common deadlock issue。
最好的解决办法是不要尽量避免到处使用ConfigureAwait(false)
,因为必须到处 ].所以如果你忘记了一个地方,或者如果有一些库代码没有使用它(遗憾的是很常见),那么你就无法避免死锁。
最好的解决办法是拥抱使用await
。将对 Task.Wait()
、Task<T>.Result
和 Task.GetAwaiter().GetResult()
的每次调用替换为 await
:
await this.Client.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(this.DatabaseName));
await this.Client.CreateDatabaseAsync(new Database { Id = this.DatabaseName });
var result = await this.Client.ReplaceDocumentAsync(this.GetDocumentLink(d.id), d, options);
I'm not using await as in an IIS context is seems to never respond.
请确保您的目标是 .NET 4.5 并且 已在 web.config
.
targetFramework
设置为 4.5
在我的例子中,有类似的任务 cancel/error、显示的内部异常 "DictionaryError" 或聚合异常。至少我发现导致问题的主要原因只是通过 http:// 使用端点到 CosmosDb 模拟器。更改后 https://localhost:8081 - 一切开始工作。