Significance of bool IsReusable in http handler interface

我的未来我决定 提交于 2019-12-02 14:13:11
AnthonyWJones

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!