views:

1137

answers:

2

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

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?

Thanks

+24  A: 

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 applciation will instance as many of these handlers as are need to handle the current load.

The downside is that if the instance values are large then instances of these handlers can cause more memory to be used. Conversely they can also reduce apparant 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.

AnthonyWJones
Excellent answer! :)
dotnetdev
+11  A: 

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

There could be a context switch at any time, even midway through executing the ProcessRequest method, so your handler cannot rely on any data which might be modified by other concurrent threads.

LukeH