I have an IHttpHandler which I believe can benefit from re-use, because it is expensive to set up, and is thread-safe. But a new handler is being created for each request. My handler is not being re-used.
Following is my simple test case, without the 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.
Am I misunderstanding how IsReusable works? Is there something else that can defeat re-use? My handler is being called from a Silverlight application if that makes any difference.
IsReusable is not a guarantee.
Just refactor your handler and put all cross-request state into a different class. Clearly separating cross-request state in a web application in best-practice anyway because it is dangerous.