0

I got a issue: I am lopping through a webservice and saving data on the database.

My code works but the issue is that wen i call it from ajax on layout chrome its crashing.

Controller:

 [HttpPost]
        public JsonResult seen()
        {
            var us = User.Identity.Name;
            var loginuserID = db.workers.Where(r => r.UserId == us).FirstOrDefault();
            var Ids = db.Clients.Select(r => r.login).ToList();

            List<apicli> list1 = new List<apicli>();
            List<notification> notifications = new List<notification>();
            var notificat = new notification();
            var clie = new apicli();
            var sales1 = new sales();
            List<sales> sales = new List<sales>();

            foreach (var item1 in Ids)
            {
                var client = new WebClient();
                var text = client.DownloadString("http://website.com/api-v2/mt4_prices.php?mode=mt4_trades&s="+item1+"&name=username&pass=password");
                var wclients = JsonConvert.DeserializeObject<dynamic>(text);
                var data = wclients;
                var deposit = wclients;
                if (!db.sales.Any(a => a.Ticket == sales1.Ticket && a.CMD == sales1.CMD))

                {
                    if (sales1.CMD == 6)
                    {
                        notifications.Add(notificat);
                        db.SaveChanges();
                    }

                }

            }



            return new JsonResult();
        }

and my ajax call on the layout its:

<script>
  function getEvents() {
        $.ajax({
            dataType: 'json',
            type: "POST",
            url: '@Url.Action("seen","Ajax")',

            cache: false,
            async: false,
            error: function (xhr, status, error) {

            },

        });
    }
    </script>
Eni Spetsi
  • 13
  • 3
  • 1
    What do you mean it's crashing? Chrome is crashing? Why didn't you include any error information? – Rick S Sep 14 '17 at 19:44
  • its crashing because its looping through 3000 records. – Eni Spetsi Sep 14 '17 at 19:45
  • Case closed, I guess. – Devin L. Sep 14 '17 at 19:46
  • Any idea on how its possible to keep ruining that json action every 10 seconds after it finish the loop without slowing the webapp? – Eni Spetsi Sep 14 '17 at 19:48
  • So you loop thru 3000 Ids and create 3000 web client and make 3000 calls to web service and make 3000 db calls. What better reason do you need for application to time out and run out of resources and get crashed. Can u explain why you have this code? – Chetan Sep 14 '17 at 19:52
  • I need to call the webapi so i check if they are new "sales" on its client,if they are the save it and add a record on ticket table so the user knows that got a new sale on one of his clients, otherwise dont save anything and continue to the new id. – Eni Spetsi Sep 14 '17 at 19:56
  • Possibly a duplicate of [C# WebClient Memory Usage](https://stackoverflow.com/q/3383167). Either dispose the `WebClient` deterministically or reuse it as shown in [Why I can't reuse WebClient to make the same request twice?](https://stackoverflow.com/q/35703777). – dbc Sep 14 '17 at 20:38
  • is it possible to make ajax call and dont wait the action to load just call it and continue without waiting a return.? – Eni Spetsi Sep 14 '17 at 20:57

0 Answers0