指定Handler中的IsReusable用处

指定Handler中的IsReusable用处?http://www.cnblogs.com/myaspnet/archive/2010/12/11/1902811.html

 

指定Handler中的IsReusable用处?

 以下代码的用处?
public bool IsReusable {
        get {
            returnfalse;
        }
    }

*****************************************
资料1:
*****************************************
IHttpHandler.IsReusable 属性
获取一个值,该值指示其他请求是否可以使用 IHttpHandler 实例。
属性值
如果 IHttpHandler 实例可再次使用,则为 true;否则为 false。
 备注
将 IsReusable 属性用所提供的重写IsReusable 属性访问器 (getter) 的代码显式设置为 true 或 false。

*****************************************
资料2:
*****************************************
IHttpHandler接口中的属性 IsReusable
Network     2008-09-25
IHttpHandler IsReusable Property
There isn't much clear info regarding this property on the net. It can bebetter in terms of performance when the property is set to true, but can causesome headaches if you have set it to true in the wrong scenario.

public bool IsReusable
{
   get
   {
      return false;
   }
}
The property is used to indicate if a single intantiation of your IHttpHandlerimplementation can be used to process multiple concurrent requests. Each clientrequest is considered to be a worker thread on the serverside when beingprocessed through the ASP.NET pipeline. So, if we set IsReusable = true, weneed to make sure that our ProcessRequest method is threadsafe. TheProcessRequest should not rely on any state that could be modified by otherconcurrent request threads. Important when your IHttpHandler implementation doesexpensive initialisations, otherwise it probabaly doesn't really matter if youreturn true or false (since simple object allocation is fairly inexpensive in.NET). N.B. Pages are never pooled.

IHttpHandler接口中的属性 IsReusable

在MSDN上对IsReusable的介绍不够清楚。当该属性设置为true,将获得更好的执行性能,但是这在某些情况会引起一些 headcache的问题。
public bool IsReusable
{
 get { return false; }
}
这 个属性用来指明,如果你IHttpHandler的实现类的单个实例被用来处理多个请求。当通过ASP.NET 管道处理时,每个客户端请求被服务端认为是一个工作者线程。因此,如果我们设置 IsReusable = true 时,我们需要确信ProcessRequest 方法是线程安全的。 ProcessRequest 应该不会依赖任何有可能被其他请求修改的状态值。当你的IHttpHandler实现类忙于做初始化时,否则你无需介意IsReusable 返回的是true 或者 false。
————————————————————————
Email:xiumang2006 AT 126.com 备案
开源目录:
http://www.itjewy.com/
*********************************************************
资料3:
*********************************************************
关于HttpHandler的可重用性的思考(关于IsReusable属性的疑惑)

原来一直对这个IHttpHandler的可重用性不太理解,今天看了一篇文章算是有一点感觉了. 总的说来针对每个请求都会由一个HttpHandlerFactory初始化一个对应的IHttpHandler的实例出来,  而这个Handler是否有状态并非取决于这个类是否有成员变量.  而是取决于它是否依赖于HttpContext.Request, 因为它被Factory初始化出来的时候是用这个context作为参数的,应该是这样理解吧.  当IsReusable 为真时, CLR会维护一个对象池, 通常是因为创建这个Handler需要较大的开销时才这么做, 同时它也应该是无状态的.

但是还是有一个问题, 就是IHttpHandler如果让它也支持象Aspx那样的输出缓存呢? 可能还需要继续研究.

那么怎么样的情况才可以重用这个handler 呢,这个就很明显了. 只要它不依赖于context.Request就可以.当然也不能有成员变量.

另外补充一下, 最好不使用.ashx文件,而直接在web.config接定相应的handler的类, 因为这些对更改handler来说更灵活一些,并且. 直接奖代码写在.ashx里面的话它得等到被请求时才会被编译, 稍微影响一点速度. 再有就是考虑到代码安全的问题.

分类: ASP.NET
 
0
0
 
(请您对文章做出评价)
 
« 博主前一篇:C#中struct和class的区别
» 博主后一篇:只要关闭浏览 器,session就消失了
原文地址:https://www.cnblogs.com/suizhikuo/p/2316587.html