How to use ConcurrentDictionary and Task in ASP.NET

I am having some issues where projectLogDict is not updating the ImageCount . If you look at the code below, I have var drawingCount and var imageCount which I add to the Task.WhenAll.

The problem is that the imageCount value never seems to get updated in projectLogDict. However if I comment out the var drawingCount, the imageCount value works and gets updated in projectLogDict

I’m not sure why both values can’t be updated in projectLogDict.

Is there a way to update both drawingCount and imageCount and add it to the 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;
    
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

When the second AddOrUpdate runs, if will not Add as the projectId key already exists, so it will run the updateValueFactory delegate. This is where your issue is. For both calls, you are only setting the one property, returning the new class which means you are constantly overriding what is already in your dictionary.

To solve this, you need to take into account value in your delegate and set the corresponding property :

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

and

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x