【问题标题】:How to use ConcurrentDictionary and Task in ASP.NET如何在 ASP.NET 中使用 ConcurrentDictionary 和 Task
【发布时间】:2021-03-08 20:38:45
【问题描述】:

我遇到了一些问题,projectLogDict 没有更新 ImageCount。如果你看下面的代码,我有var drawingCountvar imageCount,我将它们添加到Task.WhenAll

问题在于imageCount 的值似乎从未在projectLogDict 中更新。但是,如果我注释掉 var drawingCountimageCount 的值会起作用并在 projectLogDict 中更新

我不确定为什么不能在projectLogDict 中更新这两个值。

有没有办法同时更新drawingCountimageCount 并将其添加到projectLogDict 中?

public async Task<ConcurrentDictionary<string, ProjectLogs>> GetProjectFiles(ConcurrentDictionary<string, ProjectLogs> projectLogDict)
{
    var locations = await _test.GetAllLocations().ConfigureAwait(false);
    foreach(var branch in locations)
    {
        var projects = await _test.GetLocationProjectsAsync(branch).ConfigureAwait(false);

        var task = new List<Task<ConcurrentDictionary<string, ProjectLogs>>>();
        var taskTwo = new List<Task<ConcurrentDictionary<string, ProjectLogs>>>();

        foreach (var project in projects)
        {
            var drawingCount = GetDrawingCount(project.Id);
            var imageCount = GetImageCount(project.Id);
            taskTwo.Add(imageCount);
            task.Add(drawingCount);

        }

        var x = await Task.WhenAll(task.ToArray()).ConfigureAwait(false);
        var y = await Task.WhenAll(taskTwo.ToArray()).ConfigureAwait(false);
    }

    return projectLogDict;
}



public async Task<ConcurrentDictionary<string, ProjectLogs>> GetImageCount(string projectId)
{
    var files = await _test.GetUploadFilesAsync(projectId, null).ConfigureAwait(false);
    projectLogDict.AddOrUpdate(projectId, new ProjectLogs { ImageCount = file.count() }, (key, value) => value = new ProjectLogs { ImageCount = file.count() });
    
    return projectLogDict;
}



public async Task<ConcurrentDictionary<string, ProjectLogs>> GetDrawingCount(string projectId )
{
    
    var drawingCount = await _test.GetItemsDrawing(projectId).ConfigureAwait(false);
    projectLogDict.AddOrUpdate(projectId, new ProjectLogs { DrawingCount = drawingCount.Count() }, (key, value) => value = new ProjectLogs { DrawingCount = drawingCount.Count() });
    
    return projectLogDict;
    
}

【问题讨论】:

  • 您不需要 ConcurrentDictionary,因为您没有对字典进行并发写入。另外,我希望GetDrawingCountGetImageCount 有一个projectLogDict 参数。目前还不清楚他们甚至从哪里得到他们的 projectLogDict,以及它是否与 GetProjectFiles 收到的相同。这只是在等待一些令人讨厌的惊喜发生在您认为自己正在使用同一本词典的地方,但实际上并非如此。

标签: c# asp.net asynchronous task


【解决方案1】:

当第二个AddOrUpdate 运行时,如果Add 不存在,因为projectId 键已经存在,所以它将运行updateValueFactory 委托。这就是你的问题所在。对于这两个调用,您只设置一个属性,返回新类,这意味着您不断覆盖字典中已有的内容。

要解决这个问题,您需要在您的委托中考虑value 并设置相应的属性:

projectLogDict.AddOrUpdate(projectId, new ProjectLogs { ImageCount = file.count() }, (key, value) => new ProjectLogs { ImageCount = file.count(), DrawingCount = value.DrawingCount });

projectLogDict.AddOrUpdate(projectId, new ProjectLogs { DrawingCount = drawingCount.Count() }, (key, value) => new ProjectLogs { DrawingCount = drawingCount.Count(), ImageCount = value.ImageCount });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多