1

Im an developing a web application with MVC3, Razor, C# 4, jQuery.

Within one screen (page) I do AJAX calls to a controller action to get some screen-updates. Using Javascripts setTimeout() I do polling.

To optimize this polling in case where the server does not have screen-updates I like to delay the HTTP response and wait a little until I a) get screen-updates or b) hit some timeout (eg. 10 seconds or so).

I tried to do something like this

[OutputCache(Duration = 0)]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult CheckForUpdates()
{
    var startTime = DateTime.Now;
    while (_haveUpdates || DateTime.Now > startTime.AddSeconds(10   ))
    {
        Thread.Sleep(3);
    }

    return _haveUpdate ? Json(new {updates = ... }, JsonRequestBehavior.AllowGet) : null;
}

In the view I use Javascript / jQuery like this:

<script>
$(function () {
    scheduleNextCheck();
});

function scheduleNextCheck() {
    setTimeout(function() {
        $.ajax({
            url: "@Url.Action("CheckForUpdates")",
            dataType: 'json',
            data: null,
            timeout: 10000,
            cache: false,
            success: function(data) {
                if (data != null && data.updates != null ) {
                    // Apply Updates
                } else {
                    scheduleNextCheck();
                }
            },
            error: function() {
                scheduleNextCheck();
            }
        });
    }, 2000);
}

When using this code the IIS7 worker process will hang/freeze totally so only killing the worker-process can unlock the entire IIS7 server. So Thread.Sleep() seems not to be a good idea. At least not my applications environment.

Did anybody do timething like this with ASP.Net MVC yet or have any idea?

Thx, Marc

==== Update: ====

Found one of the problems: the while criteria was wrong. It should be like this:

    while (noUdates && DateTime.Now < startTime.AddSeconds(10))

The problem now is that other AJAX-requests are canceled while this sleep-delay-loop is running.

About SignalR: Have to take a closer look but the problem is that this needs additional server- and client-side libraries which I have to get approval for so I was hoping for an "easy" small solution with less impact and less need for training. But SignalR still is an option for one of the next releases to replace polling-stuff - but need to get some training and experience with this stuff first.

==== Update 2: ====

I am looking for a solutions that works without additional libraries / frameworks so I can apply it to the nearly finished web-application without a huge impact.

2 Answers 2

5

You may take a look at SignalR which is designed exactly for such situations. Instead of having the client poll the server with multiple HTTP requests, it is the server that notifies the client when updates are available.

2
  • I took a look at SignalR some time ago and got the feeling that is will only work with the upcomming MVC 4 and ASP.Net 4.5. Does it work with MVC 3 already?
    – Marc
    Jun 21, 2012 at 14:06
  • 1
    SignalR works perfectly fine with ASP.NET. It doesn't even require you to have ASP.NET MVC. You could host the SignalR hub inside any ASP.NET web application. Jun 21, 2012 at 14:09
1

I'm also looking into SignalR with my site, I'm reading this article which is pretty good: http://www.dotnetcurry.com/ShowArticle.aspx?ID=780.

From my understanding, the only real constraint is the browser version, but SignalR degrades to accommodate older versions.

There's some info here too: http://www.entechsolutions.com/browser-alerts-with-asp-net-4-5-and-signalr

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.