Bot Framework 中的错误 500,InvalidOperationException:异步操作挂起时异步模块或处理程序已完成

Error 500 in Bot Framework, InvalidOperationException: An asynchronous module or handler completed while async operation pending

我有一个使用 C#/MS Bot Framework 构建的机器人,它在 Azure 上运行。它工作正常,但 Azure 中的 HTTP 日志显示大量失败的 HTTP 请求,大约占总数的 20%。

当我启用失败的请求跟踪时,这就是我在日志中看到的:

URL: /api/messages

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace

[InvalidOperationException: An asynchronous module or handler completed while an asynchronous operation was still pending.]

我无法从日志中提取更多详细信息,包括堆栈跟踪(它是空的)。

该机器人在模拟器中也能正常运行。通过 Facebook 与机器人对话时出现失败的请求,尽管用户可以正常与机器人交互,尽管看似失败的请求。

我不知道是代码的哪一部分导致了这个问题,我的其他机器人似乎没有这个问题。

我怀疑我可能正在使用 context.Done(this) 来终止对话,但这似乎是使用它的正常做法。

任何代码的哪一部分可能导致此问题的指示或想法,或者如何提取有关错误的更多详细信息,我们将不胜感激。谢谢。

如果不看代码,很难确切地知道发生了什么,但是这种异常在 async/await 世界中很常见。通常这意味着在您的 module/handler 完成和异步方法完成之间存在竞争条件。

我会寻找已调用但未等待的异步方法。

一些相关阅读: