什么是IHttpHandler.IsReusable用? [英] What is the use for IHttpHandler.IsReusable?

查看:195
本文介绍了什么是IHttpHandler.IsReusable用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个的IHttpHandler ,我需要实现一个 IsReusable 属性。当我看 MSDN文档它说:


  

获取一个指示其他请求是否可以使用该值
  IHttpHandler的实例。


这是不是非常有帮助。 在哪些情况下我应该使用一个可重用的处理程序,并在哪些情况下应该是不被重用?

跟进的问题:


  1. 什么是重复使用?

  2. 我能保持状态(即类变量),当可重复使用= TRUE ??


解决方案

此属性指示,如果多个请求可以使用相同的IHttpHandler实例处理。通过在请求管道末端默认被放置在一个HttpApplication的handlerRecycleList所有HTTP处理程序设置为null。如果处理程序是可重复使用的它不会被设置为null,实例将在接下来的请求被重用。

主要增益性能,因为那里将是垃圾收集较少的对象。结果
最重要的痛点可重复使用的处理程序是,它必须是线程安全的。这是不平凡的,需要一些努力。

我个人建议你保留默认的行为(不能重用),如果你只使用管理的资源,因为垃圾收集器应该很容易处理它们。相比于引进很难找到线程错误的风险,可重复使用的处理器的性能增益通常是可以忽略的。

如果您决定重用处理程序,你应该避免维护,因为如果处理程序实例同时访问多个请求将写入/读取值类变量的状态。

I'm writing a IHttpHandler and I'll need to implement a IsReusable property. When I look at the MSDN documentation it says:

Gets a value indicating whether another request can use the IHttpHandler instance.

This isn't very helpful. In which situations should I use a reusable handler and in which situations should it not be reusable?

Follow up questions:

  1. What is reuse?
  2. Can I maintain state (i.e. class variables) when Reusable = true??

解决方案

This property indicates if multiple requests can be processed with the same IHttpHandler instance. By default at the end of a request pipeline all http handlers that are placed in the handlerRecycleList of the HttpApplication are set to null. If a handler is reusable it will not be set to null and the instance will be reused in the next request.

The main gain is performance because there will be less objects to garbage-collect.
The most important pain-point for reusable handler is that it must be thread-safe. This is not trivial and requires some effort.

I personally suggest that you leave the default behavior (not reusable) if you use only managed resources because the Garbage Collector should easily handle them. The performance gain from reusable handlers is usually negligible compared to the risk of introducing hard to find threading bugs.

If you decide to reuse the handler you should avoid maintaining state in class variables because if the handler instance is accessed concurrently multiple requests will write/read the values.

这篇关于什么是IHttpHandler.IsReusable用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆