3

I'm writing some code where, most commonly, no results will be returned from a query against Entity Framework. This request has been submitted by some jQuery code, and if I reply with "no results", it's just going to turn around and make the same request again - so I'd like to not respond until either some results are available, or a reasonable amount of time (e.g. 30 seconds) have passed (however, I don't want to cache results for 30 seconds - 30 seconds is a reasonable amount of time to not send a response to the query - if results become available, I want them available "immediately")

How do I best go about this. I tried sleeping between re-querying, but it a) doesn't seem to be working (every request that starts with no results waits the full 30 seconds), and b) will tie up an asp.net thread.

So how do I convert my code to not tie up asp.net threads, and to respond once 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);
    }
}
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448

2 Answers2

4

check out this mix 11 session: "Pragmatic JavaScript jQuery & AJAX with ASP.NET". At the very end of it (about 40-45 minutes into the session) there is a demo right for you.
I'm prety sure you'll say wow..
Damian Edwards promissed to post more about the technique on his blog, but we are yet to see it..

Alexander Taran
  • 6,655
  • 2
  • 39
  • 60
  • Yea I saw this one too, it was a great session, very much worth watching – BrokenGlass Apr 29 '11 at 21:46
  • Just watched that. 'tis good, but it would mean moving away from EF/Linq, I'll have to mull it over (plus, at the moment, I'm using CE as my backing SQL, and I don't think it supports dependencies). Thanks though. – Damien_The_Unbeliever Apr 30 '11 at 09:25
  • well you don't have to move away, but you'll have to write some raw sql queries. Concerning the CE thing - it's exactly what i wrote to Damian to keep in mind when he will write that blog post. – Alexander Taran Apr 30 '11 at 16:40
0

See > Reverse ajax Comet/Polling implementation for ASP.NET MVC?.

You need to go with long polling. It basically sends a request to the server and the server just keeps it in the queue. It accumulates all the queries and as soon as it receives some data it sends the response to each of the queued requests.

EDIT: Also this is interesting > Comet implementation for ASP.NET?

Community
  • 1
  • 1
neebz
  • 11,465
  • 7
  • 47
  • 64
  • How would the EF side of this work - wouldn't I need to continually poll the DB? – Damien_The_Unbeliever Apr 29 '11 at 16:17
  • Yea I think you have to poll that in regular intervals. There is a way of sending a HTTP POST request from SQL Server using Triggers but it's widely used and apparently not thread safe. – neebz Apr 29 '11 at 16:21