我需要调用一个返回 Task
的方法从内部
public override void OnActionExecuting(ActionExecutingContext filterContext)
它不会让我使这个方法异步它会抛出以下内容
An asynchronous module or handler completed while an asynchronous operation was still pending.
当打电话时
entityStorage.GetCurrentUser().Result
我陷入僵局。我怎样才能避免这种情况?
我一直在玩弄它想出的东西
entityStorage.GetCurrentUser().Result.ConfigureAwait(false).GetAwaiter().GetResult();
但这不起作用。我该怎么做?我的解决方案需要使用 ASP.NET 4 和 Async Targetting Pack,我不能在部署到 Azure 时使用 ASP.NET 4.5。
最佳答案
由于 await 只是编译器为您重写延续的语法糖,因此最“直接”的路径是采用将要跟随您的 await 的任何代码并使其成为 ContinueWith 调用。
所以,像这样:
entityStorage.GetCurrentUser().ContinueWith(t =>
{
// do your other stuff here
});
关于asp.net - 在不使用 await 的情况下调用异步方法时防止死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11887915/