Significance of bool IsReusable in http handler interface

asp.netHttphandlerIhttphandler

asp.net Problem Overview


When writing a http handler/module, there is an interface member to implement called - bool IsReusable.

What is the significance of this member? If I set it to false (or true), what does this mean for the rest of the web app?

asp.net Solutions


Solution 1 - asp.net

The normal entry point for a handler is the ProcessRequest method. However you may have code in the class constructor which puts together some instance values which are expensive to build.

If you specify Reusable to be true the application can cache the instance and reuse it in another request by simply calling its ProcessRequest method again and again, without having to reconstruct it each time.

The application will instantiate as many of these handlers as are need to handle the current load.

The downside is that if the number of instances needed is larger than the instances currently present, they cause more memory to be used. Conversely they can also reduce apparent memory uses since their instance value will survive GC cycles and do not need to be frequently re-allocated.

Another caveat is you need to be sure that at the end of the ProcessRequest execution the object state is as you would want for another request to reuse the object.

Solution 2 - asp.net

Further to AnthonyWJones's answer, if your HTTP handler returns true for IsReusable then you should ensure that it is fully thread-safe.

There's nothing in the documentation to indicate that reusable handlers can't be reused concurrently, although the current Microsoft implementations only appear to reuse them consecutively. But, at least in theory, a single handler instance could be reused simultaneously by multiple requests, so you shouldn't rely on any data which might be modified by other concurrent threads.

Solution 3 - asp.net

If you don't store any state in that instance (i.e.: you don't have any fields (aka "class variables")) then you should be safe reusing it.

It's by default false to be on the safe side.

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
QuestionGurdeepSView Question on Stackoverflow
Solution 1 - asp.netAnthonyWJonesView Answer on Stackoverflow
Solution 2 - asp.netLukeHView Answer on Stackoverflow
Solution 3 - asp.netAndrei RîneaView Answer on Stackoverflow