public async Task <FileContentResult> DownloadAsync(int year, int month) { try { var employeeErrors = new HashSet <string>(); var departments = _storage.GetDepartmentsAsync(); var departmentEmployees = new Dictionary <string, IList <Employee> >(); var fillTasks = new List <Task>(); await foreach (var employee in _storage.GetEmployeesAsync()) { fillTasks.Add(FillSalaryAsync(employee, employeeErrors)); if (!departmentEmployees.ContainsKey(employee.DepartmentId)) { departmentEmployees.Add(employee.DepartmentId, new List <Employee>()); } departmentEmployees[employee.DepartmentId].Add(employee); } await Task.WhenAll(fillTasks); var date = new DateTime(year, month, 1); var report = _reportBuilder.Create(new DateTime(year, month, 1), await departments, departmentEmployees, employeeErrors); return(new FileContentResult(Encoding.UTF8.GetBytes(report), "application/octet-stream") { FileDownloadName = $"report_{date:yyyy-MM}.txt", }); } catch (Exception e) { _logger.LogError(e, $"error for {year}-{month}"); throw; } }