Task.WhenAll给出错误

时间:2014-04-03 07:49:44

标签: c# multithreading asp.net-mvc-4 async-await

public async virtual Task<ActionResult> Store(int? id, int? mainRoadID, int? segmentID, int? cityid, string serverMessage = "")
{
    UserTrafficReport_Create model = await Task.WhenAll(GetNewModel, InitialCameras, GetMonitoredWaysListAndPushViewData(mainRoadID, segmentID, cityid));            
    return View(model);
}

上一个函数有错误行...我找不到确切的错误

  

错误1358'System.Threading.Tasks.Task.WhenAll(params System.Threading.Tasks.Task [])'的最佳重载方法匹配有一些无效的参数

这些是When When

中使用的三个函数
    public async virtual Task<UserTrafficReport_Create> GetNewModel(int? id, int? mainRoadID, int? segmentID, int? cityid)
    {
        var model = new UserTrafficReport_Create();
        var serializer = new JavaScriptSerializer();
        if (id != null && id > 0)
        {
            var report = _repository.FindOne<UserTrafficReport>((int)id);
            model.InjectFrom(report);
            model.Comments = report.Comments;
            if (report.PictureSize.HasValue && report.PictureSize > 0)
                model.photo_name = report.ID.ToString(CultureInfo.InvariantCulture);

            if (report.RoadID != null)
            {
                model.RoadID = (int)report.RoadID;
                _repository.FindOne<MonitoredWay>((int)report.RoadID);
            }
            FakeUsers(report.UserID);
            model.RoadStatus = report.RoadStatus ?? 99;
            if (report.traffic_rating >= 0)
                model.traffic_rating = report.traffic_rating;
            else
                model.traffic_rating = null;
        }
        else
        {
            var fakeGroup = _repository.First<UserGroup>(g => g.Name.Contains("Fake"));
            var fakeGroupId = 3;
            if (fakeGroup != null)
                fakeGroupId = fakeGroup.ID;

            var dbNamesList = (from userAD in _context.UserAdditionalDatas
                               join groups in _context.UserMultiGroups on userAD.ID equals groups.UserDataId
                               join aspUser in _context.AspnetUsers on userAD.ID equals aspUser.ID
                               where (groups.UserGroupId == fakeGroupId)
                               select new
                               {
                                   name = userAD.FirstName,
                                   UserName = aspUser.Username,
                                   userId = aspUser.ID
                               }).Distinct().ToList();

            if (dbNamesList.Any())
            {
                var randomedList = dbNamesList.Randomize();
                var fakeUser = randomedList.FirstOrDefault();
                if (fakeUser != null)
                {
                    model.GuestName = fakeUser.name;
                    model.UserID = fakeUser.userId;
                }
            }

            model.RoadID = segmentID.GetValueOrDefault(-1);
            model.traffic_rating = -1;
            if (cityid != null)
                model.CityId = (int)cityid;
        }
        return model;
    }

    public async virtual Task InitialCameras(int? cityid,string serverMessage = "")
    {
        var serializer = new JavaScriptSerializer();
        var conditionslist = CreateListFromSingle(
                   new
                   {
                       value = "99",
                       text = "Not Specified"
                   }
            );
        conditionslist.Add(new { value = "4", text = "Accident" });
        conditionslist.Add(new { value = "2", text = "Danger" });
        conditionslist.Add(new { value = "3", text = "Road Work" });

        string outputOfConditions = serializer.Serialize(conditionslist);
        ViewData["ConditionsListSerialized"] = outputOfConditions;
        var conditionslistitems =
         (from condition in conditionslist
          select new SelectListItem
          {
              Value = condition.value,
              Text = condition.text
          }).ToList();
        ViewBag.ConditionsList = conditionslistitems;
        ViewData["serverMsg"] = serverMessage;
        if (cityid == null || cityid == -1)
        {
            var cameras = _context.Cameras.Select(c => new
            {
                value = c.Id,
                text = c.Name
            }).ToList();
            cameras.Insert(0, new { value = (long)0, text = "--Select a Camera --" });
            ViewData["Cameras"] = serializer.Serialize(cameras);
        }
        else
            ViewData["Cameras"] = GetCityCameras((int)cityid);
    }

...

    private async Task GetMonitoredWaysListAndPushViewData(int? roadID = null, int? segmentID = null, int? cityID = null, Guid? reporterId = null)
    {
        int? id = cityID;
        var dbWaysList =
            _context.MonitoredWays.Where(
                m =>
                    !m.IsTest &&
                    (m.RoadID != null && m.Road.AppOrder >= 0 && (id <= 0 || id == null)
                        ? m.Road.AreaID > 0
                        : m.Road.AreaID == id));

        var xWayseSelectList = (from s in dbWaysList
                                select new
                                {
                                    OppId = s.OppositeSegment ?? 0,
                                    Value = s.ID,
                                    Title = s.EnglishName,
                                    RoadTitle = s.Road.EnglishName
                                }).ToList().Distinct();

        var repsList = (from s in xWayseSelectList//context.MonitoredWays
                        select new SelectListItem
                            {
                                Value = s.Value.ToString(CultureInfo.InvariantCulture),
                                Text = string.IsNullOrEmpty(s.RoadTitle) ? s.Title : s.RoadTitle + " (" + s.Title + ")",
                                Selected = segmentID != null && (segmentID.Value == s.Value)
                            }).Distinct().ToList();



        var serializer = new JavaScriptSerializer();
        string wayseSelectListOppId = serializer.Serialize(xWayseSelectList);
        string outputOfAreas = serializer.Serialize(repsList);
        ViewData["MonitoredWaysListSerialized"] = outputOfAreas;
        ViewData["OppositeMonitoredWays"] = wayseSelectListOppId;

        ViewBag.MonitoredWaysList = repsList;

        var conditionslist = CreateListFromSingle(
                   new
                   {
                       value = "99",
                       text = "Not Specified"
                   }
            );
        conditionslist.Add(new { value = "4", text = "Accident" });
        conditionslist.Add(new { value = "2", text = "Danger" });
        conditionslist.Add(new { value = "3", text = "Road Work" });

        string outputOfConditions = serializer.Serialize(conditionslist);
        ViewData["ConditionsListSerialized"] = outputOfConditions;
        var conditionslistitems =
         (from condition in conditionslist
          select new SelectListItem
          {
              Value = condition.value,
              Text = condition.text
          }).ToList();
        ViewBag.ConditionsList = conditionslistitems;

        var ratingslist = CreateListFromSingle(
       new
               {
                   value = "0",
                   text = "V. Bad"
               }
        );
        ratingslist.Add(new { value = "1", text = "Bad" });
        ratingslist.Add(new { value = "2", text = "Average" });
        ratingslist.Add(new { value = "3", text = "Good" });
        ratingslist.Add(new { value = "3", text = "V. Good" });
        ViewBag.Ratingslist = ratingslist;
        string outputOfRatings = serializer.Serialize(ratingslist);
        ViewData["RatingsListSerialized"] = outputOfRatings;

        if (roadID != null)
        {
            var rod = _context.Roads.FirstOrDefault(r => r.ID == roadID);
            if (rod != null)
            {
                cityID = rod.AreaID;
            }
        }

        var dbAreassList = _context.Cities.ToList();
        var areas =
            (from area in dbAreassList
             select new SelectListItem
             {
                 Value = area.ID.ToString(CultureInfo.InvariantCulture),
                 Text = area.EnglishName,
                 Selected = cityID != null && (cityID.Value == area.ID)
             }).ToList();
        ViewBag.AreasList = areas;
        var areasList = (from s in _context.Cities
                         select
                             new
                             {
                                 id = s.ID,
                                 text = s.EnglishName
                             }).ToList();

        serializer = new JavaScriptSerializer();
        string outputOfAreas1 = serializer.Serialize(areasList);
        ViewData["AreasListSerialized"] = outputOfAreas1;
        var fakeGroup = _repository.First<UserGroup>(g => g.Name.Contains("Fake"));
        var fakeGroupId = 3;
        if (fakeGroup != null)
            fakeGroupId = fakeGroup.ID;

        var dbNamesList = (from userAD in _context.UserAdditionalDatas
                           join groups in _context.UserMultiGroups on userAD.ID equals groups.UserDataId
                           join aspUser in _context.AspnetUsers on userAD.ID equals aspUser.ID
                           where (groups.UserGroupId == fakeGroupId)
                           select new
                           {
                               Text = userAD.FirstName,
                               Value = userAD.ID,
                               Selected = false
                               //Email = aspUser.Username
                           }).Distinct().ToList();
        var namess = dbNamesList.Select(s => new SelectListItem
                                                 {
                                                     Text = s.Text,
                                                     Value = s.Value.ToString(),
                                                     Selected = s.Selected
                                                 }).ToList();
        if (reporterId != null)
        {
            var member = _repository.FindOne<UserAdditionalData>((Guid)reporterId);
            if (member != null)
            {
                namess.Add(new SelectListItem
                               {
                                   Text = member.FirstName,
                                   Value = member.ID.ToString(),
                                   Selected = true
                               });
            }
        }

        var random = new Random();
        if (!namess.Any(n => n.Selected))
        {
            int rand = random.Next(0, namess.Count - 1);
            namess[rand].Selected = true;
        }

        ViewBag.FakeUsersList = namess;
    }

1 个答案:

答案 0 :(得分:1)

这一行有些不妥之处:

UserTrafficReport_Create model =
           await Task.WhenAll(
                      GetNewModel,
                      InitialCameras,
                      GetMonitoredWaysListAndPushViewData(mainRoadID, segmentID, cityid));    

Task.WhenAllTask个实例的集合作为参数。

  1. 你传递了两个代表和一个任务。您可能打算实际调用前两种方法,以便他们返回任务?
  2. Task.WhenAll返回Task。等待该任务不会返回任何内容,因此您将无法为model分配任何内容。
  3. Task<UserTrafficReport_Create> modelFactoryTask = GetNewModel(...);
    
    await Task.WhenAll(
              modelFactoryTask ,
              InitialCameras(...),
              GetMonitoredWaysListAndPushViewData(mainRoadID, segmentID, cityid));
    
    UserTrafficReport_Create model = modelFactoryTask.Result;