IHttpHandler IsReusable,但没有被重用

Mar*_*ker 5 c# asp.net silverlight

我有一个IHttpHandler,我相信它可以从重用中受益,因为它设置起来很昂贵,并且是线程安全的.但是正在为每个请求创建一个新的处理程序.我的处理程序没有被重用.

以下是我的简单测试用例,没有昂贵的设置.这个简单的案例说明我的问题:

public class MyRequestHandler : IHttpHandler
{
    int nRequestsProcessed = 0;

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        nRequestsProcessed += 1;
        Debug.WriteLine("Requests processed by this handler: " + nRequestsProcessed);
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }
}

Requests processed by this handler: 1
Requests processed by this handler: 1
Requests processed by this handler: 1
Requests processed by this handler: 1... at least 100 times. I never see > 1.
Run Code Online (Sandbox Code Playgroud)

我误解了IsReusable的工作原理吗?还有其他东西可以打败重复使用吗?我的处理程序是从Silverlight应用程序调用的,如果这有任何区别的话.

usr*_*usr 3

IsReusable 并不能保证。

只需重构您的处理程序并将所有交叉请求状态放入不同的类中即可。无论如何,以最佳实践清楚地分离 Web 应用程序中的交叉请求状态是很危险的。