IHttpHandler IsReusable but not reused

I have an IHttpHandler that I think can benefit from reuse because it is expensive to set up and is thread safe. But for each request a new handler is created. My handler is not reused.

Below is my simple test case without expensive setup. This simple case demonstrates my problem:

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.

I don’t understand how IsReusable works? Is there anything else that reuse can defeat? My handler is called from a Silverlight application, if that matters.

+5
source share
2 answers

IsReusable is not a guarantee.

. - , .

+3

, , .

IsReusable true, :

  • .
  • ProcessRequest.
  • .

, , ( ) - URI, , .

, , ( ), .

, , .

, . , ProcessRequest .

, , IHttpHandlerFactory:

public class MyRequestHandlerFactory : IHttpHandlerFactory
{
  private static MyRequestHandler SingletonHandler = new MyRequestHandler();
  IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
  {
    return SingletonHandler;
  }
  void ReleaseHandler(IHttpHandler handler)
  {
    //do nothing
  }
}

Web.Config, MyRequestHandlerFactory, MyRequestHandler, .

( , , - oopsie!)

+1

All Articles