I have a web API method that gets a dashboard view.
The method calls about 24 different queries. Each query taking about 60ms to execute, I'm using Glimpse to profile.
What I was hoping to do, was run them asynchronously as to avoid the calling of each one, one after the other, thus making it 60ms X 5 method calls.
I'm also new to Async Await, so my expectations may be incorrect.
Here is my Web API Method
[HttpGet]
[ExceptionHandling]
public async Task<DashboardResponse> GetDashboard()
{
return await DashboardResponse.GetDashboard();
}
Here is the helper method
public static async Task<DashboardResponse> GetDashboard()
{
var resp = new DashboardResponse();
resp.MonthGlance = await GetMonthAtAGlance();
resp.MostRecentOrder = await GetMostRecentOrder();
resp.CreateAnOrder = await GetCreateAnOrder();
resp.RecentOrders = await GetRecentOrders();
resp.RecentNotifications = await GetRecentNotifications();
var messages = MessageResponse.GetMessages(new MessageFilters() { PageNumber = 1, PageSize = 10 }).Messages;
resp.RecentMessages.Messages = messages;
resp.OrderLineChart = GetOrderLineChart();
return resp;
}
Here is one of the methods called inside the helper method (the rest are setup very similar)
public static async Task<MonthGlanceResp> GetMonthAtAGlance()
{
var monthAtAGlance = new MonthGlanceResp();
var monthStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
var monthEnd = monthStart.AddMonths(1).AddDays(-1);
var monthGlanceQuery = Helpers.DbContext.Current.Orders
.Where(c => c.Status != OrderStatuses.Deleted &&
(c.OrderSubSource == OrderSubSources.ApplicantLink ||
c.OrderSubSource == OrderSubSources.BulkImport ||
c.OrderSubSource == OrderSubSources.OrderComprehensive) &&
c.DateIn >= monthStart &&
c.DateIn <= monthEnd).AsQueryable();
if (UserContext.Current.AccountAccess == CustomerAccessTypes.Global)
{
monthGlanceQuery = monthGlanceQuery.Where(c => c.CustomerId == UserContext.Current.CustomerId);
}
else if (UserContext.Current.AccountAccess == CustomerAccessTypes.Account)
{
monthGlanceQuery = monthGlanceQuery.Where(c => c.CustomerAccountId == UserContext.Current.CustomerAccountId);
}
else
{
monthGlanceQuery = monthGlanceQuery.Where(c => c.CustomerAccountPersonId == UserContext.Current.CustomerAccountPersonId);
}
monthAtAGlance.ClearOrderCount = await monthGlanceQuery
.CountAsync(c => c.OrderLineItems
.Where(d => d.ItemType == "package_service" || d.ItemType == "service")
.All(d => d.Result == OrderLineItemResults.Clear && d.Status == OrderLineItemStatuses.ApprovedForClient));
monthAtAGlance.QuestionableOrderCount = await monthGlanceQuery
.CountAsync(c => c.OrderLineItems
.Where(d => d.ItemType == "package_service" || d.ItemType == "service")
.All(d => d.Status == OrderLineItemStatuses.ApprovedForClient) &&
c.OrderLineItems
.Where(d => d.ItemType == "package_service" || d.ItemType == "service")
.Any(d => d.Result == OrderLineItemResults.Questionable));
monthAtAGlance.PendingOrderCount = await monthGlanceQuery
.CountAsync(c => c.OrderLineItems
.Where(d => d.ItemType == "package_service" || d.ItemType == "service")
.Any(d => d.Status != OrderLineItemStatuses.ApprovedForClient));
monthAtAGlance.OrderCount = await monthGlanceQuery.CountAsync();
return monthAtAGlance;
}
The only problem is that after implementing all the async await changes it appears that the web api call is now running slower than before! i'm not sure if I have it structured correctly, or even if what i'm thinking is possible.