h1

IsReusable on the IHttpHandler interface

November 1, 2007

I saw a recent interesting question about HttpHandlers on the ASP.NET forums that I wanted to share:

“What is the purpose of the IsReusable() method on the IHttpHandler interface?”

Well, to answer it we have to understand that a HttpHandler is processed as part of the request life cycle in ASP.NET and for every one of those requests an instance of the appropriate HttpHandler is required.

The IsReusable property enables you to indicate that the same instance can be pooled and used by concurrent requests.

This is useful when you have a lot of initialisation code occurring inside the handler that does not have to be repeated for every single worker thread and request.
For example:

public class MyHandler : IHttpHandler
{
public MyHandler()
{
//constructor logic involves
// a lot of processing and object instantiation
}

#region IHttpHandler Members
public void ProcessRequest(HttpContext context)
{
context.Response.Write(“biscuit”);
}

public bool IsReusable
{
get { return true; }
}
#endregion
}

This illustrates a simple example where the actual processing is writing some text to the context.

So why do we have to worry about whether it is true or false?

We need to make it Thread safe

If you are indicating that the HttpHandler can be re-used then it needs to be programmed with thread safety in mind.
Concurrent requests that can interfere with object state can cause havoc with the results of the handler’s use when it is not thread safe.

Here is an example:

public class MyHandler : IHttpHandler
{

protected
string exampleString;

public MyHandler()
{
//constructor logic involves
// a lot of processing and object instantiation
}

#region IHttpHandler Members
public void ProcessRequest(HttpContext context)
{
exampleString = MyUtility.FromUrl(context.Request.Url.ToString() );

context.Response.Write(exampleString);
}

public bool IsReusable
{
get { return true; }
}
#endregion
}

Between the two lines in the ProcessRequest method there is an opportunity for context switching to interfere with the processing of the handler.
If concurrent requests are using this handler, as one request has retrieved and set the exampleString variable based on url parameters a context switch to a second request thread at the same point means that exampleString is changed to a new value.
When processing switches back to the original request thread, it writes out a potentially different value than the one intended.
Ouch.

Such a problem would probably cause “intermittent” failures and a problem that was potentially hard to pin down.
So if in doubt about thread safety, set the IsReusable method to false.

Otherwise, if you are sure it is thread safe, set to true and pool the handler for potential increased performance in high volume environments.

Advertisements

4 comments

  1. […] Original post by foreachbiscuit […]


  2. Nice post – well to the point. Cheers mate.


  3. […] I have it set to false, but this topic is an entire blog post entirely. Check out this link for a good […]


  4. That’s the best to-the-point post I have found on the topic. Good post.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: