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?
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?
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.
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.
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. –
Raper 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. –
Raper IHttpHandler
and IHttpHandlerFactory
is extremely light on detail. @Frederic is correct that the default Microsoft implementations only appear to reuse handlers consecutively, not concurrently [...] –
Tetrad 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. –
Tetrad 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. –
Raper 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.
© 2022 - 2024 — McMap. All rights reserved.