12

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.

MatthewMartin
  • 32,326
  • 33
  • 105
  • 164
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

4 Answers4

15

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.

Rex M
  • 142,167
  • 33
  • 283
  • 313
  • 2
    I could never find proper MSDN documentation for this one :/ – IrishChieftain May 29 '12 at 20:35
  • @IrishChieftain also for this question which ive asked ( and didnt get any good asnwer) -lousy msdn documenetation ... http://stackoverflow.com/questions/8298843/registerclientscriptblock-parameters-usages-in-real-scenarios – Royi Namir May 29 '12 at 20:37
  • Take a look at Branislav's response here: http://stackoverflow.com/questions/1507572/streaming-databased-images-using-httphandler – IrishChieftain May 29 '12 at 20:40
  • @IrishChieftain so it actually trying to make it static alike ? – Royi Namir May 29 '12 at 20:43
  • @RoyiNamir if I understand your meaning, then yes. It is taking a singleton-like approach where it will hold a single static instance of your HttpHandler and reuse it over and over. – Rex M May 29 '12 at 20:44
  • what do you mean `OK to share `? then handler will get 1+2 and should return 3. ( example). what am i sharing here ? it should calc a+b for each a&b i send him.... – Royi Namir May 29 '12 at 20:44
  • That was always my take on it but there is a total lack of documentation on MSDN on this property so best approach would be to create our own test cases to determine exactly what is happening... I haven;t actually done that myself. – IrishChieftain May 29 '12 at 20:44
  • @RoyiNamir OK to share between requests. Request A and Request B from two different users can have the same instance of your handler process their requests. – Rex M May 29 '12 at 20:45
  • I cant find andy real scenario.... lets say the handler is doing calc according to complex math function which is taken from DB....ok..... according to your answer - i will only take the formulla once from DB. but EACH REQUEST will ahve to run a NEW CALC based on its inputs....so? – Royi Namir May 29 '12 at 20:48
  • @RexM can you please answer my last comment ? – Royi Namir May 29 '12 at 21:01
  • @RoyiNamir your example is a good one. Fetching some one-time data from a DB and holding it as an instance member in your handler class means your handler is probably reusable, which saves on performance. – Rex M May 29 '12 at 21:21
  • @rex ok : 2 question please : 1) why not make it static 2) resuseable till when ? – Royi Namir May 29 '12 at 21:22
  • @RoyiNamir to answer both questions - the purpose of this is to delegate instance lifecycle management to ASP.NET so you don't have to worry about it. The ASP.NET request pipeline is actually remarkably efficient and smart about handling performance optimally for most scenarios. The best use of your time is to rely on it, and don't worry about it. If you do run into a weird situation where it's just not doing the right thing for your needs, at that point it might make sense to implement your own approach using statics. – Rex M May 29 '12 at 21:25
  • @RexM I know perfectlly well how GC is working...:) but my question is : does maing it Reuseable - makes it not candidate for GC ? ( still no one is referencing it....**meanwhile**) - there's a situation in which in 5 minutes - a request will come.... so he cant be disposed... so i guess it has a constatnt ROOT which prevent him from Gc'ed.....what do you say? – Royi Namir May 30 '12 at 06:51
  • @RoyiNamir I don't know the internals, based on the minimal description from MSFT I expect it to hold a single instance for the life of the AppDomain. To me the more important question is, why does it matter? – Rex M May 30 '12 at 12:37
1

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.

Jeggs
  • 250
  • 1
  • 3
0

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.

Chris Disley
  • 1,286
  • 17
  • 30
0

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;
    }
}
IrishChieftain
  • 15,108
  • 7
  • 50
  • 91