asp.nethttphandlerihttphandler

Significance of bool IsReusable in http handler interface


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?


Solution

  • 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.