I am working to reduce latency and increase responsiveness for my application. Within on of my API Controllers (Asp .NET Core)
I am using an HttpClient
to perform several requests to a server, fetching incident and task information.
I perform two requests per group a user is associated with (I fetch this information using Microsoft Graph API); one for incidents, one for tasks.
I then await their results and if they do not return null, append them to my dictionary object under the group's name.
Return Object:
Group 1
-- Incidents ArrayList
-- Tasks ArrayList
Group 2
-- Incidents ArrayList
-- Tasks ArrayList
...
My code is as follows:
Dictionary<string, ArrayList> final;
Microsoft.Graph.Group group_details;
string group_name;
using (var client = ClientFactory.GetClientAsync(_apiCredentials))
{
//for each group send request to get past 30 day's incidents and tasks
foreach (var group in groups.CurrentPage)
{
//request to Graph API to get name of group for query
try
{
group_details = await _graphServiceClient.Groups[group.Id].Request()
.GetAsync();
group_name = group_details.DisplayName.Replace("#", "").Trim();
//creates async tasks for fetching Incidents and Tasks
var getIncidentsTask = GetIncidents(client, startDate, group_name);
var getTasksTask = GetTasks(client, startDate, group_name);
//awaits values
var Incidents = await getIncidentsTask;
var Tasks = await getTasksTask;
//if both Incidents and Tasks exist for current group
if (Incidents != null && Tasks != null)
{
final.Add(group_name, new ArrayList());
final[group_name].Add(Incidents);
final[group_name].Add(Tasks);
}
}
catch(Exception e)
{
_logger.LogError(e.Message);
}
}
}
I know there exists a more advanced way to await these tasks. I was attempting to utilize Task.WhenAll()
, however this will only work with an IEnumerable
so I have to dig a layer in and loop through each group while using this statement to generate a task per completion of each set of incidents/tasks.
My goal is to have all tasks run to completion in the background while as few threads are blocked as possible, then if each set of results are not null, append them to my return object.
If anyone has advice on this I would be very appreciative.