I write code where, as a rule, query results from the Entity Framework are not returned. This query was sent using some jQuery code, and if I answer "no results", it will simply expand and repeat the same query - so I would not want to respond until any results are available, or a reasonable amount of time (for example , 30 seconds) has passed (however, I do not want to cache the results for 30 seconds - 30 seconds - this is a reasonable amount of time not to send a response to the request - if the results become available, I want them to be available "immediately")
How am I best to do this. I tried to sleep between the repeated request, but it does not work (every request that starts without any results, waits a full 30 seconds), and b) will bind the asp.net stream.
So, how do I convert my code so that I don't bind asp.net streams and respond as soon as the results are available?
[HttpGet] public ActionResult LoadEventsSince(Guid lastEvent, int maxEvents) { maxEvents = Math.Min(50, maxEvents); //No more than 50 using (var dbctxt = new DbContext()) { var evt = dbctxt.Events.Find(lastEvent); var afterEvents = (from et in evt.Session.Events where et.OccurredAt > evt.OccurredAt orderby et.OccurredAt select new { EventId = et.EventId, EventType = et.EventType, Control = et.Control, Value = et.Value }).Take(maxEvents); var cycles = 30; while (afterEvents.Count() == 0 && cycles-- > 0) { System.Threading.Thread.Sleep(1000); } return Json(afterEvents.ToArray(), JsonRequestBehavior.AllowGet); } }
source share