开发者

Running Multiple WebRequests at the same time with $.ajax

I have 2 MVC apps that need to connect to a 3rd application. The code that is causing problems is identical in both MVC apps.

    public bool UpdateServerData()
    {
            //string BASE_URL = "http://";
            string BASE_URL = "http://localhost:55094";
            string url = BASE_URL + "/Home/PullLiveData";
            WebRequest wr = WebRequest.Create(url);
            wr.Credentials = CredentialCache.DefaultNetworkCredentials; // uses current windows user
            var response = (HttpWebResponse)wr.GetResponse();
            return true;
    }

    public ActionResult GetServerUpdateProgress()
    {
            //string BASE_URL = "http://";
            string BASE_URL = "http://localhost:55094";
            string url = BASE_URL + "/Home/UpdateProgress";
            WebRequest wr = WebRequest.Create(url);
            wr.Credentials = CredentialCache.DefaultNetworkCredentials; // uses current windows user

            var myobj = new UpdateAJAXProgress();
            var response = (HttpWebResponse)wr.GetResponse();
            var reader = new StreamReader(response.GetResponseStream());
            JavaScriptSerializer js = new JavaScriptSerializer();
            var objText = reader.ReadToEnd();
            myobj = (UpdateAJAXProgress)js.Deserialize(objText, typeof(UpdateAJAXProgress));
            return Json(myobj);
        }

UpdateServerData tells localhost:55094 to refresh the data in the db. GetServerUpdateProgress returns a progress count / total so that I can display a progress bar for the db refresh.

The $.ajax that runs UpdateServerData is set to work async: true

Then using setTimeouts, I run GetServerUpdateProgress every few seconds which returns how far along UpdateServerData is from completing.

Working App Functionality:

Running Multiple WebRequests at the same time with $.ajax

Not Working:

Running Multiple WebRequests at the same time with $.ajax

So what appears to be happening in the non-working version is that it is not running GetServerUpdateProgress even though ajax is calling up the function, it doesn't actually begin to process anything inside it until UpdateServerData is complete. You can see the ajax is sync is set correctly because the loading animation is displayed for both in the non working screen shot. I put a breakpoint at the start of GetServerUpdateProgress and it doesn't even trigger until after UpdateServerData is finished processing.

My main concern is that some setting, perhaps in the webconfig or global.asax, is different and that is what is causing both these apps to run a little differently. Any ideas?

Here's some of the associated javascript:

function UpdateData()
{
        $("#ChartUpdateProgressWhole").show();

    $.ajax({
        type: "POST",
        async: true,
        url: '@(Url.Action("UpdateServerData", "Charts"))',
        contentType: "application/json; charset=utf-8",
        succes开发者_运维百科s: function (data) {
            Alert2({
                iconfilename: "tick.png",
                text: "Database Updated!",
                autohide: true,
            });
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {

        },
        complete:function (jqXHR, textStatus) {
            $("#ChartUpdateProgressWhole").hide();
            timerStop = true;
            LoadChart();
        },
    });

    if (!timerStop) {
        CheckProgress();
        }
}
function CheckProgress()
{
    if(timerStop) {
        CheckProgressAjax();
        return;
    }
    window.setTimeout(CheckProgress, 2000);
    CheckProgressAjax();
}

function CheckProgressAjax()
{
    $.ajax({
        type: "POST",
        async: false,
        url: '@(Url.Action("GetServerUpdateProgress", "Charts"))',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var width = (data.Completed * 100)/data.Total;
            $("#ChartUpdateProgressPercent").css("width", width);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.status);
            alert(XMLHttpRequest.responseText);
        }
    });
}


Taking a bit of a stab in the dark here, but ASP.NET will actually queue async requests and process them in order if session state is enabled (to avoid having to deal with synchronisation issues no doubt).

My suggestion would be to disable session state all together if feasible, if this is not possible you can disable it per controller using the following attribute:

[SessionState(SessionStateBehavior.Disabled)]
public class SomeController
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜