133

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?

Kols
  • 3,641
  • 2
  • 34
  • 42
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
  • [Real world usage of IHttpHandler.IsReusable Property](http://stackoverflow.com/questions/10806214/ihttphandler-isreusable-property-truly-usages) – Sen Jacob Mar 02 '13 at 06:54

3 Answers3

154

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.

jdehaan
  • 19,700
  • 6
  • 57
  • 97
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • 2
    You mention object reuse as being the key optimization achieved by specifying IsReusable=True. Will setting IsReusable=False result in the server not instantiating multiple instances of the handler ever. I.E. - does it eliminate concurrent requests? – Ian Nov 10 '15 at 21:10
  • @Ian I believe it then functions as a normal page when reusable is false, though I may be wrong. When it's reusable it may be able to handle 6 concurrent requests with 2 or 3 instances (example only) whereas when it is not reusable it would need 1:1 instances/processes. I can't imagine .Net would let requests pile up as 1 worker thread tried to handle it all while continuously refreshing/reinitialising its state. – DavidScherer Jan 04 '19 at 15:57
47

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.

LukeH
  • 263,068
  • 57
  • 365
  • 409
  • Sorry for being thick, but could somebody please elaborate on what is meant by “context switch”. If you access things from the sesson or query string (content.Request.QueryString) is that reusable or not? – zod Jun 09 '11 at 12:37
  • 5
    Context switching is when a CPU stops processing on one thread and starts processing on another. I.E. the CPU switched it's context from one thread to another. This happens constantly in PCs, it gave us the illusion of multitasking before there were multicore computers. – Larry Dukek Sep 26 '11 at 16:14
  • I could not understand when u said **There could be a context switch at any time**. When we type url and press enter, this executes one request at a time. right ? –  May 24 '13 at 04:51
  • The context switching will be an issue when multiple users are using an application at the same time. For example, if two users want to update the same record at the same time, the context switch could cause problems. – Ishmael Smyrnow Jun 13 '13 at 13:58
  • 7
    This statement about `IsReusable` requiring thread safety seems in contradiction with AnthonyWJones response. As I understand its third paragraph (The application will instantiate as many of...), a reusable handler instance will not be reused concurrently, but only after having finish its current processing. If this is true, then there is no need to be thread safe. – Frédéric Jul 04 '14 at 14:36
  • @Frederic - You are right in the sense that the class itself need not be thread-safe since there is 1 thread per instance (assuming that's what AWJ means by "The application will instantiate as many of these handlers as are needed to handle the current load"). However, I think Luke just meant that the shared resources accessed from within the ProcessRequest execution need to be thread-safe; like if you are reading and writing to static/global variables. I.E. - if a thread checks a DB field at the start of Process Request, don't assume it won't be changed before the thread exits the handler. – Ian Nov 10 '15 at 21:05
  • @Ian, whatever the value of `IsReusable`, shared resources must be handled in a thread safe way. So even if you guess right about what he wanted to mean, it looks wrong anyway. – Frédéric Nov 12 '15 at 11:58
  • @Frederic, @Ian: The documentation for [`IHttpHandler`](https://msdn.microsoft.com/en-us/library/system.web.ihttphandler.aspx) and [`IHttpHandlerFactory`](https://msdn.microsoft.com/en-us/library/system.web.ihttphandlerfactory.aspx) is extremely light on detail. @Frederic is correct that the default Microsoft implementations only appear to reuse handlers consecutively, not concurrently *[...]* – LukeH Nov 12 '15 at 14:07
  • *[...]* but it's not difficult to imagine (or write) a `IHttpHandlerFactory` implementation that always dishes out the same singleton handler instance if `IsReusable` is true. Such a factory wouldn't be breaking the interface contract, as far as I can tell, and in that situation the handler could definitely be reused concurrently and so would need to be fully thread-safe. – LukeH Nov 12 '15 at 14:13
  • You're right for `IHttpHandler`, looking at the documentation, nothing guarantees it will not be used concurrently (interface doc and `IsReusable` property doc). And relying on undocumented features is unsafe. On `IHttpHandlerFactory` side, `ReleaseHandler` method suggests that this undocumented feature is the only usage pattern 'forecast' by those interfaces designers. This remains quite a weak hint. – Frédéric Nov 12 '15 at 15:13
  • I eventually read this question (http://stackoverflow.com/questions/5500950/what-is-the-use-for-ihttphandler-isreusable). The accepted answer by Mr. Branislov implies that multiple threads concurrently executing the same instance of the handler is possible. So I'll just code assuming that. I'm guessing MS makes the documentation vague so they can change their implementation later. – Ian Nov 12 '15 at 15:39
3

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.

Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166