是什么导致此特定方法陷入僵局?

贾斯汀·赫尔格森

尽我所能,我一直选择异步。但是,我仍然无法使用不是为异步构建的ASP.NET Membership。结果,我对类似方法的调用string[] GetRolesForUser()无法使用异步。

为了正确地建立角色,我依赖于来自各种来源的数据,因此我正在使用多个任务来并行获取数据:

public override string[] GetRolesForUser(string username) {
   ...
   Task.WaitAll(taskAccounts, taskContracts, taskOtherContracts, taskMoreContracts, taskSomeProduct);
   ...
}

所有这些任务仅是使用实体框架从SQL Server数据库中获取数据。但是,最后一个任务(taskSomeProduct的引入导致了死锁,而其他方法都没有。

这是导致死锁的方法:

public async Task<int> SomeProduct(IEnumerable<string> ids) {
    var q = from c in this.context.Contracts

            join p in this.context.Products
            on c.ProductId equals p.Id

            where ids.Contains(c.Id)

            select p.Code;

    //Adding .ConfigureAwait(false) fixes the problem here
    var codes = await q.ToListAsync();
    var slotCount = codes .Sum(p => char.GetNumericValue(p, p.Length - 1));

    return Convert.ToInt32(slotCount);
}

但是,此方法(看起来与所有其他方法非常相似)不会导致死锁:

public async Task<List<CustomAccount>> SomeAccounts(IEnumerable<string> ids) {
    return await this.context.Accounts
        .Where(o => ids.Contains(o.Id))
        .ToListAsync()
        .ToCustomAccountListAsync();
}

我不太确定导致死锁的一种方法是什么。最终,他们俩都在执行查询数据库的相同任务。添加ConfigureAwait(false)一个方法确实可以解决问题,但是我不确定是什么与其他执行得很好的方法有什么区别。

编辑

这里是一些其他代码,为简洁起见,我最初省略了它们:

public static Task<List<CustomAccount>> ToCustomAccountListAsync(this Task<List<Account>> sqlObjectsTask) {
    var sqlObjects = sqlObjectsTask.Result;
    var customObjects = sqlObjects.Select(o => PopulateCustomAccount(o)).ToList();
    return Task.FromResult<List<CustomAccount>>(customObjects);
}

PopulateCustomAccount方法CustomAccount仅从数据库Account对象返回一个对象。

usr

ToCustomAccountListAsync你打Task.Result那是一个经典的僵局。使用await

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

是什么导致此特定方法陷入僵局?

来自分类Dev

为什么这会导致Go陷入僵局?

来自分类Dev

为什么我在MySQL中陷入僵局

来自分类Dev

Cuda Mutex,为什么会陷入僵局?

来自分类Dev

是什么导致此错误?

来自分类Dev

是什么导致此TypeError?

来自分类Dev

是什么导致此随机空白?

来自分类Dev

是什么导致此SQLite CursorWindowAllocationException?

来自分类Dev

是什么导致此Maven / JBehave错误?

来自分类Dev

是什么导致此NullPointer异常?

来自分类Dev

是什么导致此modx xpdo错误?

来自分类Dev

是什么导致此modx错误?

来自分类Dev

是什么导致此Apache 401错误?

来自分类Dev

为什么所有goroutine都陷入僵局?

来自分类Dev

为什么我的dispatch_once陷入僵局?

来自分类Dev

为什么我的dispatch_once陷入僵局?

来自分类Dev

Go程序陷入僵局

来自分类Dev

是什么导致在VBA Excel中出现“对象不支持此属性或方法”的消息?

来自分类Dev

是什么导致此Hash:Class的未定义方法“ model_name”

来自分类Dev

芹菜任务可能陷入僵局?

来自分类Dev

在Java代码中陷入僵局

来自分类Dev

是什么导致此div内的填充/空白?

来自分类Dev

是什么导致此“无隐式转换”错误?

来自分类Dev

是什么导致此sqlite外键不匹配?

来自分类Dev

是什么导致此“可能的精度损失”错误?

来自分类Dev

是什么在rvm下安装rubinius时导致此错误?

来自分类Dev

Python newb:是什么导致此函数无法打印?

来自分类Dev

是什么导致此WildFly / Undertow管道破裂错误?

来自分类Dev

是什么会导致此属性偶尔抛出NullReferenceException?