When is a Generic HttpHandler (an ashx, the IHttpHandler interface) reusable?

╄→гoц情女王★ 提交于 2019-12-04 19:06:01

问题


I've been using Ashx along with jQuery. I've read msdn, I'm talking about the IHttpHandler.IsReusable Property.

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

What do they mean "the IHttpHandler instance."? Are they trying to make it alike static for everyone to see and use ? Is it reusable by the same what ? ( QueryString, cookies, etc?)

If I write this:

public class MyHttpHandler : IHttpHandler
   {
      public void ProcessRequest(HttpContext context)
      {
         context.Response.Write(DateTime.Now.Ticks.ToString());      
      }

      public bool IsReusable
      {
         get { return true; }
      }
   }

it appears each request will get its own up-to-date - Datetime value.


回答1:


In your example, you're not managing any state. Anyone can call ProcessRequest as many times as they want on the same instance and they will get a new DateTime.

But what if you did this:

private DateTime _dateTime = DateTime.Now;

public void ProcessRequest(HttpContext context)
{
    context.Response.Write(_dateTime);
}

Now you'll get the same response every time after the handler instance is instantiated. Unless ASP.NET generates a new one every time.

IsReusable can indicate whether your handler class manages any state that is OK to share between separate requests. If it manages state that isn't OK to share, then it's not likely to be externally idempotent - or possibly even not thread-safe. Calling ProcessRequest with the same input conditions may not result in the same output, because your specific handler implementation also has some instance-level variables that are not OK to share when it determines the output. (And in fact, technically your current implementation is an example of that). In these cases the instance is probably not "reusable" - and ASP.NET needs to generate a new instance each time to ensure predictability.

So for cases where you don't manage state, or you have very simple handlers where the state is "obvious" (like the example we have here), IsResuable might seem pointless. But if you have a very complex handler - possibly one that does keep some state that's expensive to initialize but OK to share - you would want to indicate to ASP.NET it's OK to reuse it for performance.




回答2:


Sorry to just post a link to the answer, but a great explanation of IsReusable:

IsReusable blog post

Essentially turn it on if your code is completely thread safe, leave it off if it's not. Well thats what I gather from it.




回答3:


If you have an instance variable in there I think you'll find it's shared across numerous calls, so if your date time there was set as an instance variable and you just return that in your processrequest I think it'll return same value, whereas it won't if isreusable = false as it'll create a new instance of your handler.




回答4:


Apparently, this keeps the handler in memory and able to handle multiple requests. When set to false, it has to create a new instance of the handler for each incoming request.

public bool IsReusable
{
    get
    {
        return true;
    }
}


来源:https://stackoverflow.com/questions/10806214/when-is-a-generic-httphandler-an-ashx-the-ihttphandler-interface-reusable

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