如何在 ASP.NET 中使用 ConcurrentDictionary 和 Task
How to use ConcurrentDictionary and Task in ASP.NET
我遇到了一些问题,其中 projectLogDict
没有更新 ImageCount
。如果您查看下面的代码,我将 var drawingCount
和 var imageCount
添加到 Task.WhenAll
.
问题是 imageCount
值似乎从未在 projectLogDict
中更新过。但是,如果我注释掉 var drawingCount
,imageCount
值有效并在 projectLogDict
中更新
我不确定为什么不能在 projectLogDict
中更新两个值。
有没有办法同时更新 drawingCount
和 imageCount
并将其添加到 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;
}
当第二个AddOrUpdate
运行s时,如果不会Add
因为projectId
键已经存在,所以会运行updateValueFactory
代表。这就是你的问题所在。对于这两个调用,您只设置一个 属性,返回新的 class,这意味着您不断地覆盖字典中已有的内容。
要解决这个问题,您需要在委托中考虑 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 });
我遇到了一些问题,其中 projectLogDict
没有更新 ImageCount
。如果您查看下面的代码,我将 var drawingCount
和 var imageCount
添加到 Task.WhenAll
.
问题是 imageCount
值似乎从未在 projectLogDict
中更新过。但是,如果我注释掉 var drawingCount
,imageCount
值有效并在 projectLogDict
我不确定为什么不能在 projectLogDict
中更新两个值。
有没有办法同时更新 drawingCount
和 imageCount
并将其添加到 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;
}
当第二个AddOrUpdate
运行s时,如果不会Add
因为projectId
键已经存在,所以会运行updateValueFactory
代表。这就是你的问题所在。对于这两个调用,您只设置一个 属性,返回新的 class,这意味着您不断地覆盖字典中已有的内容。
要解决这个问题,您需要在委托中考虑 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 });