ASP.Net:IHttpAsyncHandler 和 IRequiresSessionState 不起作用

发布于 2024-08-26 22:13:11 字数 1974 浏览 2 评论 0原文

我已经实现了 IHttpAsyncHandler。我正在从一个包含该处理程序小部件的网页进行大约 5 个不同的 AJAX 调用。

其中一个小部件大约需要 15 秒才能加载(因为数据库查询很大),其他小部件应该在一秒钟内全部加载完毕。处理程序以同步方式响应。

我得到的结果非常不一致。 ProcessRequest 方法使用 Session 和其他类级别变量。 这会导致不同的请求使用同一个线程,而不是每个请求都有自己的线程吗?

我得到这个...

Request1 --->响应1秒
请求2 ---->响应 14 秒
请求3 --->响应 14.5 秒
请求4 --->响应15秒
请求5 --->响应 15.5 秒

但我正在寻找更像这样的东西...

Request1 --->响应1秒
请求2 ---->响应 14 秒
请求3 --->响应1.5秒
请求4 --->响应2秒
请求5 --->响应 1.5 秒

在没有发布太多代码的情况下,我对 IHttpAsyncHandler 方法的实现非常标准。

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); 
}

在我的 IHttpAsyncHandler.BeginProcessRequest 方法中放置一个调试断点,我可以看到该方法在最后一个 Process 完成之前不会被触发。

我的 machine.config 也有这个条目... processModel autoConfig="true" 未设置其他属性。

我这样调用处理程序...
$.ajax( { 类型:“获取”, url: "../HTML/HtmlHandler.ashx", 缓存:真, 数据类型:“文本”, 数据:{ html:名称}, 成功:函数(html){ //$(函数() { //console.log(选项卡名称); //console.log("msg:" + msg); $("#" + 姓名 + "持有者").html(html); //检查所有已加载(); ClientHome_init(''); //}); },

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

我还需要检查什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慕烟庭风 2024-09-02 22:13:11

以防万一其他人遇到此问题,将 IRequiresSessionState 更改为 IReadOnlySessionState 可以解决我的问题。

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

财迷小姐 2024-09-02 22:13:11

您的问题可能不在于您的服务器代码。 JavaScript 的本质是它是同步的。即使您使用异步 AJAX 请求,我发现 javascript 也很少允许您同时发出两个 HTTP 请求。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文