What is the use for IHttpHandler.IsReusable?

C#asp.netasp.net MvcIhttphandlersystem.web

C# Problem Overview


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??

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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:

https://stackoverflow.com/questions/1507572/streaming-databased-images-using-httphandler

Solution 3 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKees C. BakkerView Question on Stackoverflow
Solution 1 - C#Branislav AbadjimarinovView Answer on Stackoverflow
Solution 2 - C#IrishChieftainView Answer on Stackoverflow
Solution 3 - C#spenderView Answer on Stackoverflow