gpt4 book ai didi

c# - 调用 ReplaceDocumentAsync 方法失败返回

转载 作者:太空宇宙 更新时间:2023-11-03 21:10:43 25 4
gpt4 key购买 nike

在 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()。但是方法没有返回,线程最终没有返回就关闭了。

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 并且已设置 targetFramework在你的 web.config 中达到 4.5 .

关于c# - 调用 ReplaceDocumentAsync 方法失败返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37929637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com