ASP.Net: IHttpAsyncHandler and IRequiresSessionState not working

北战南征 提交于 2019-12-08 13:10:48

问题


I have implemented an IHttpAsyncHandler. I am making about 5 different AJAX calls from a webpage that has widgets to that handler.

One of those widgets takes about 15 seconds to load(because of a large database query) the others should all load in under a second. The handler is responding in a synchronous manner.

I am getting very inconsistent results. The ProcessRequest method is using Session and other class level variables. Could that be causing different requests to use the same thread instead each there own?

I'm getting this...

Request1 ---> response 1 sec
Request2 ---> response 14 sec
Request3 ---> response 14.5 sec
Request4 ---> response 15 sec
Request5 ---> response 15.5 sec

but I'm looking for something more like this...

Request1 ---> response 1 sec
Request2 ---> response 14 sec
Request3 ---> response 1.5 sec
Request4 ---> response 2 sec
Request5 ---> response 1.5 sec

Without posting too much code my implementation of the IHttpAsyncHandler methods are pretty standard.

private AsyncProcessorDelegate _Delegate;

protected delegate void AsyncProcessorDelegate(HttpContext context); 

IAsyncResult IHttpAsyncHandler.BeginProcessRequest(HttpContext context, 
    AsyncCallback cb, object extraData)
{
    _Delegate = new AsyncProcessorDelegate(ProcessRequest);

    return _Delegate.BeginInvoke(context, cb, extraData); 
}

void IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
{
    _Delegate.EndInvoke(result); 
}

Putting a debug break point in my IHttpAsyncHandler.BeginProcessRequest method I can see that the method isn't being fired until the last Process is complete.

Also my machine.config has this entry... processModel autoConfig="true" with no other properties set.

I'm calling the handler like this...
$.ajax( { type: "GET", url: "../HTML/HtmlHandler.ashx", cache: true, dataType: "text", data: { html: name }, success: function(html) { //$(function() { //console.log(tabname); //console.log("msg:" + msg); $("#" + name + "holder").html(html); //checkAllLoaded(); ClientHome_init(''); //}); },

    error: function(XMLHttpRequest, textStatus, errorThrown) {
    $("#" + name + "holder").html("<span>Error retrieving widget.</span>");
        //console.log("error:" + tabname);
        //checkAllLoaded();
    }
}

What else do I need to check for?


回答1:


Just incase someone else has this problem changing IRequiresSessionState to IReadOnlySessionState fixed the problem for me.




回答2:


Your problem here is probably not with your server code. The nature of javascript is that it is synchronous. Even when you are using asynchronous AJAX requests, I find that javascript will rarely allow you to make two HTTP requests at the same time.



来源:https://stackoverflow.com/questions/2526168/asp-net-ihttpasynchandler-and-irequiressessionstate-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!