Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm trying to implement long polling in asp.net mvc 3 with asynchronous controllers, but my problem is that i'm unable to navigate from the page or create other ajax requests during the polling. Asynchronous controllers are a new topic to me so my questions are is it supposed to work like this or am i doing something wrong? The codes are below.

JavaScript
$(window).load(function () {
    setTimeout(function () {
        doLongPolling();
    }, 1000);
});

function doLongPolling() {
    $.post("/AsyncTest/LongPoll", null, function (data) {
        alert(data.msg);
        doLongPolling();
    });
}



C#
public class AsyncTest : AsyncController
    {
        public void LongPollAsync()
        {
            AsyncManager.OutstandingOperations.Increment();
            Thread.Sleep(20000);
            AsyncManager.Parameters["msg"] = "polling done";
            AsyncManager.OutstandingOperations.Decrement();
        }

        public ActionResult LongPollCompleted(string msg)
        {
            return this.Json(new { msg = msg });
        }
    }
Posted

Hi Huotari,

Async controllers in MVC are designed to allow you to dispatch work when you need a result.

What you're describing is more likely suited to some kind of worker pattern, then to use the polling idea to check on the status of the queue/results, but not to wait for the work. Just create a thread that pushes the work to a queue and create a worker service that runs every so often to pickup work and process it.

Also, attach firebug or use your developer tools in IE and have a look at your network traffic. ASP.NET MVC uses convention, so "/AsyncTest/LongPoll" won't likely work because you didn't name your controller AsyncTestController (check for 404 on your server).

Cheers.
 
Share this answer
 
Instead of polling from the client, your should consider server push when data becoming available. Learn about SignalR[^]
 
Share this answer
 
C#
[NoAsyncTimeout]
public void LongPollAsync(int id)
{
     AsyncManager.OutstandingOperations.Increment();
     if (GetNew(id) != null)
     {
          AsyncManager.Parameters["msg"] = GetNew(id);
          AsyncManager.OutstandingOperations.Decrement();
     }
     else 
     {
          Thread.Sleep(2000);
          LongPollAsync(id);
          AsyncManager.OutstandingOperations.Decrement();
     }
           
}

public string GetNew(int id) {
{
    xEntities db = new xEntities();
    Notification n = db.Notification.Where(x => x.ID > id).FirstOrDefault();
    if (n != null)
    {
        return n.URL;
    }
    else { return null; }
}
catch
{
     return null; 
}
}
        public ActionResult LongPollCompleted(string msg)
        {
            return this.Json(new { msg = msg });
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900