107

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??
Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
  • 9
    Have a look here: http://foreachbiscuit.wordpress.com/2007/11/01/isreusable-on-the-ihttphandler-interface/ – Homam Mar 31 '11 at 13:53
  • "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. – IrishChieftain Mar 31 '11 at 16:52
  • @IrishChieftain It's like .Net keeps a bag of instantiated HttpHandlers, picks one and just feeds it a context? – Kees C. Bakker Mar 31 '11 at 16:55
  • If set to true, it feeds the requests to the "same" handler. What is your app trying to do? – IrishChieftain Mar 31 '11 at 16:58
  • 1
    @IrishChieftain 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. – Kees C. Bakker Mar 31 '11 at 17:03
  • 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 :) – IrishChieftain Mar 31 '11 at 17:15
  • 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). – Kees C. Bakker Mar 31 '11 at 17:26
  • @Kees, per Homam's link, I agree. Concurrent requests may have to be handled with care. – IrishChieftain Mar 31 '11 at 17:51
  • 1
    possible duplicate of [Significance of bool IsReusable in http handler interface](http://stackoverflow.com/questions/539302/significance-of-bool-isreusable-in-http-handler-interface) – Pekka Jul 10 '13 at 09:13

3 Answers3

96

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.

Branislav Abadjimarinov
  • 5,101
  • 3
  • 35
  • 44
  • What are the implications for my class? – Kees C. Bakker Mar 31 '11 at 14:27
  • 1
    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? – Ben Amada Dec 15 '12 at 04:55
  • 2
    Sure. I've rephrased the last recommendation stressing that it is applicable only if the IHttpHandler is reused. Thanks for the clarification, Ben! – Branislav Abadjimarinov Dec 15 '12 at 12:16
  • 1
    Can you please explain this sentence **The most important pain-point for reusable handler is that it must be thread-safe.** with an example ? –  May 24 '13 at 05:21
  • 8
    For what it's worth, I have implemented many, many `IHttpHandler`s 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. – dana Apr 15 '14 at 00:46
  • 7
    Great explanation. Clear and to the point. I wish MSDN articles were this concise. – AlexVPerl Jun 17 '15 at 22:21
  • 1
    @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? – Ian Nov 10 '15 at 21:16
  • @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". – pholpar May 09 '19 at 11:35
  • @pholpar: Yes, I reworded the text to reflect that. – Branislav Abadjimarinov Feb 10 '20 at 08:23
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

Community
  • 1
  • 1
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
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.

spender
  • 117,338
  • 33
  • 229
  • 351