예제 #1
0
        public void GetDataFromUrl()
        {
            int        howManyRequestsAtATime = 10;
            HttpSocket socket        = new HttpSocket();
            var        matchDataList = new List <MatchData>();

            Console.WriteLine("Total matches to request: {0}", IDMatches.Count);
            while (IDMatches.Count > 0)
            {
                var idMatches = TakeAndRemove(IDMatches, howManyRequestsAtATime);

                var urls      = GetListOfUrls(idMatches);
                var tasks     = urls.Select(socket.GetAsync).ToArray();
                var completed = Task.Factory.ContinueWhenAll(tasks, completedTasks =>
                {
                    foreach (var result in completedTasks.Select(t => t.Result))
                    {
                        if (!string.IsNullOrEmpty(result))
                        {
                            MatchData match = JsonConvert.DeserializeObject <MatchData>(result);
                            matchDataList.Add(match);
                        }
                    }
                });
                completed.Wait();
            }

            foreach (MatchData match in matchDataList)
            {
                if (match != null)
                {
                    AddMatch(match);
                }
            }

            MatchesContext.Configuration.AutoDetectChangesEnabled = false;
            MatchesContext.Configuration.ValidateOnSaveEnabled    = false;
            int count = 0;

            foreach (InitialDataUpload.Models.Match match in Matches)
            {
                MatchesContext.Matches.Add(match);
                count++;

                if (count % 100 == 0)
                {
                    count = 0;
                    MatchesContext.SaveChanges();
                }
            }

            MatchesContext.SaveChanges();
        }
예제 #2
0
        public void GetDataFromUrl()
        {
            int howManyRequestsAtATime = 10;
            HttpSocket socket = new HttpSocket();
            var matchDataList = new List<MatchData>();

            Console.WriteLine("Total matches to request: {0}", IDMatches.Count);
            while (IDMatches.Count > 0)
            {
                var idMatches = TakeAndRemove(IDMatches, howManyRequestsAtATime);

                var urls = GetListOfUrls(idMatches);
                var tasks = urls.Select(socket.GetAsync).ToArray();
                var completed = Task.Factory.ContinueWhenAll(tasks, completedTasks =>
                                    {
                                        foreach (var result in completedTasks.Select(t => t.Result))
                                        {
                                            if (!string.IsNullOrEmpty(result))
                                            {
                                                MatchData match = JsonConvert.DeserializeObject<MatchData>(result);
                                                matchDataList.Add(match);
                                            }
                                        }
                                    });
                completed.Wait();
            }

            foreach(MatchData match in matchDataList)
            {
                if(match != null)
                {
                    AddMatch(match);
                }
            }

            MatchesContext.Configuration.AutoDetectChangesEnabled = false;
            MatchesContext.Configuration.ValidateOnSaveEnabled = false;
            int count = 0;
            foreach(InitialDataUpload.Models.Match match in Matches)
            {
                MatchesContext.Matches.Add(match);
                count++;

                if(count % 100 == 0)
                {
                    count = 0;
                    MatchesContext.SaveChanges();
                }
            }

            MatchesContext.SaveChanges();
        }