加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 在调用异步方法时不使用等待来防止死锁

发布时间:2020-12-15 20:42:11 所属栏目:asp.Net 来源:网络整理
导读:我需要调用从内部返回一个Task的方法 public override void OnActionExecuting(ActionExecutingContext filterContext) 它不会让我使这个方法异步它抛出以下 An asynchronous module or handler completed while an asynchronous operation was still pendin
我需要调用从内部返回一个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,我不能使用ASP.NET 4.5作为部署到Azure.

解决方法

由于等待只是语法糖为编译器重写一个延续为您,最“直接”的路径将是采取任何代码将遵循您的等待,并使其成为一个ContinueWith调用.

所以,像:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读