$ P $,而无需使用调用异步方法时pventing死锁的await [英] Preventing a deadlock when calling an async method without using await

查看:103
本文介绍了$ P $,而无需使用调用异步方法时pventing死锁的await的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用返回任务的方法从内部

public override void OnActionExecuting(ActionExecutingContext filterContext)

这不会让我做这种方法异步它抛出以下

It wont let me make this method async it throws the following

这是异步模块或处理程序,同时异步完成
  操作仍然悬而未决。

An asynchronous module or handler completed while an asynchronous operation was still pending.

和打电话时

 entityStorage.GetCurrentUser().Result

我得到一个僵局。我怎样才能避免这种情况?

I get a deadlock. How can I avoid this?

我一直在玩弄它想出的东西像

I have been playing around with it coming up with stuff like

entityStorage.GetCurrentUser().Result.ConfigureAwait(false).GetAwaiter().GetResult();

但是,这是行不通的。我该怎么办呢?我的解决方案将需要与ASP.NET 4和异步目标定位包上班​​,我不能使用ASP.NET 4.5的上午部署到Azure上。

But this isn't working. How can I do it? My solution will need to work with ASP.NET 4 and the Async Targetting Pack, I can't use ASP.NET 4.5 as am deploying to Azure.

推荐答案

由于AWAIT是编译器重写为你延续只是语法糖,最直接路径将采取一切code正想按照你的await,并使其成为ContinueWith电话。

Since await is just syntax sugar for the compiler rewriting a continuation for you, the most 'direct' path would be to take whatever code was going to follow your await and make it a ContinueWith call.

那么,是这样的:

entityStorage.GetCurrentUser().ContinueWith(t =>
{
    // do your other stuff here
});

这篇关于$ P $,而无需使用调用异步方法时pventing死锁的await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆