What is the use for IHttpHandler.IsReusable?
Asked Answered
E

3

109

I'm writing a IHttpHandler and I'll need to implement a IsReusable property. When I look at the MSDN documentation it says:

Gets a value indicating whether another request can use the IHttpHandler instance.

This isn't very helpful. In which situations should I use a reusable handler and in which situations should it not be reusable?

Follow up questions:

  1. What is reuse?
  2. Can I maintain state (i.e. class variables) when Reusable = true??
Effusive answered 31/3, 2011 at 13:51 Comment(9)
Have a look here: foreachbiscuit.wordpress.com/2007/11/01/…Edison
"Reuse" means handle more than one request with a particular instance of the handler. You can store anything in Session - it will not be affected when instance of handler is released.Inexistent
@Inexistent It's like .Net keeps a bag of instantiated HttpHandlers, picks one and just feeds it a context?Effusive
If set to true, it feeds the requests to the "same" handler. What is your app trying to do?Inexistent
@Inexistent right now I'm using it for a simple streamer of some log information. But I'm trying to learn what the implications are to understand when and how to use IHttpHandlers. They are a forgotten feature by many.Effusive
If you're reading just one log at a time as a stream, just set it to false... this topic really does need to be looked at closer. For me, all sites I develop that show lists of products use this handler. Keep on eye on Branislav's answer in the related question :)Inexistent
My understanding as of now is that the class - when IReusable returns true - should never contain any state. It should just be a handler for stuff (almost like a static).Effusive
@Kees, per Homam's link, I agree. Concurrent requests may have to be handled with care.Inexistent
possible duplicate of Significance of bool IsReusable in http handler interfaceLiterator
F
98

This property indicates if multiple requests can be processed with the same IHttpHandler instance. By default at the end of a request pipeline all http handlers that are placed in the handlerRecycleList of the HttpApplication are set to null. If a handler is reusable it will not be set to null and the instance will be reused in the next request.

The main gain is performance because there will be less objects to garbage-collect.
The most important pain-point for reusable handler is that it must be thread-safe. This is not trivial and requires some effort.

I personally suggest that you leave the default value (not reusable) if you use only managed resources because the Garbage Collector should easily handle them. The performance gain from reusable handlers is usually negligible compared to the risk of introducing hard to find threading bugs.

If you decide to reuse the handler you should avoid maintaining state in class variables because if the handler instance is accessed concurrently multiple requests will write/read the values.

Faulkner answered 31/3, 2011 at 14:3 Comment(9)
What are the implications for my class?Effusive
Your last recommendation to avoid maintaining state in class variables is slightly confusing. Assuming you set IsReusable to false, then wouldn't there be no possibility of another request (concurrent or not) accessing the same instance of that HttpHandler, and therefore maintaining state in class variables would be safe?Econah
Sure. I've rephrased the last recommendation stressing that it is applicable only if the IHttpHandler is reused. Thanks for the clarification, Ben!Faulkner
Can you please explain this sentence The most important pain-point for reusable handler is that it must be thread-safe. with an example ?Spring
For what it's worth, I have implemented many, many IHttpHandlers with IsReusable set to true and have had no issues. The main thing to keep in mind is not to have any variables scoped to the class, but rather use local variables in your functions.Jahdal
Great explanation. Clear and to the point. I wish MSDN articles were this concise.Planet
@Branislav - If I understand your answer right. It is not only possible for different threads to utilize different instances of the handler concurrently, but is also possible for different threads to be executing the SAME instance of the handler concurrently?Cyma
@BranislavAbadjimarinov: I might understand it wrong, but since IHttpHandler is an interface, and not a base class, I think we cannot read about a "default behavior". I assume you meant rather "common behavior".Lubricator
@pholpar: Yes, I reworded the text to reflect that.Faulkner
I
11

Apparently, this keeps the handler in memory and able to handle multiple requests. When set to false, it has to create a new instance of the handler for each incoming request.

Here's a question that shows what happens when it's not used properly:

Streaming Databased Images Using HttpHandler

Inexistent answered 31/3, 2011 at 13:57 Comment(2)
Do you have some documentation backing your answer?Effusive
Like you I could find no satisfactory documentation on MSDN so I had to test loading of images from a database to a page of an E-Commerce site, and then observe what happened :)Inexistent
A
5

It's cheaper to recycle the handler than to new one up every time a request comes in and the server will chum less memory, easing the work GC has to perform. If the handler is in a state where dealing with a new request would not be problematic (i.e. any state in the handler instance has been reset), then it should qualify as being reusable.

EDIT

I'm not sure if my answer correctly defines what reuse is. It actually allows for concurrent reuse, so effectively state would be best avoided or carefully managed in a thread-safe manner.

Alon answered 31/3, 2011 at 13:55 Comment(1)
I would think so too. You're right, the actual (fundamental) question is: 'what is reuse?'Effusive

© 2022 - 2024 — McMap. All rights reserved.