9

IsReusable Property

Below is my understanding for IsReusable Property

If the handler returns static content. it is safe to set the value to true. But if the thread returns dynamic content, to make it thread safe, IsReusable should be set to set as false.

In such case the Context Switching may occur, which may cause the handler to give wrong output.

Confusions

Context Switching says - Switching of one thread to another thread is called switching. right?

Paragraph 2 says context switching may occur - I think, when you send the request. That time only one context creates, which results in a Response. right? So, How Context Switching is possible. Can you give an example?

SMC
  • 237
  • 1
  • 5
  • 29
  • 1
    A link to the property in question would be nice – Panagiotis Kanavos Jul 09 '13 at 14:57
  • 3
    As I get correct you asked about [IHttpHandler.IsReusable Property](http://msdn.microsoft.com/en-us/library/system.web.ihttphandler.isreusable.aspx). You are not first who meets trouble with understanding this property. You could check [next answer and comments of ](http://stackoverflow.com/a/5501138/182344). In my opinion @Branislav Abadjimarinov explained this property in clever and simple way. – RredCat Jul 10 '13 at 19:12
  • What's your purpose exactly? – Amirhossein Mehrvarzi Jul 23 '13 at 03:21

1 Answers1

2

The question what the handler "returns" (better phrased: what content the handler writes) has nothing to do with the IsReusable property. This property makes a statement about the thread-safety of your code, not about whether the content can change. For example, a handler that writes DateTime.Now would be reusable. A handler that has an SqlConnection field and reads unchanging data would not be reusable because the connection is not thread-safe even if the data read is always the same.

Context switching also has nothing to do with this because on a multi-core box no context switch is necessary to cause concurrency. What you mean is "thread-safety" with respect to concurrent invocations of ProcessRequest on the same instance of your IHttpHandler derived class.

Now some practical advice: always have IsReusable return false and ensure that your handler class is cheap to allocate and does not bring tons of garbage with it. GC'ing a single object is nothing! My guess is the IsReusable property was created to give ASP.NET an artificial advantage in toy benchmarks, or to support poorly architected handlers that are expensive to create.

If you have expensive resources (like caches) store them elsewhere (in a static field maybe).

An easy way to obtain thread-safety is to not share anything. In that sense, don't share the handler.

TL;DR: Set IsReusable to false and move on. Nothing to see here. This is just a confusing design flaw in ASP.NET.

usr
  • 168,620
  • 35
  • 240
  • 369
  • **Paragraph 2 says context switching may occur - I think, when you send the request. That time only one context creates, which results in a Response. right? So, How Context Switching is possible.** Can you explain this part in the question ? – Imad Alazani Jul 27 '13 at 13:14
  • Context switching is a special case of concurrency problems. Context switching can occur at *any* time and multiple times. This is true for all threads, not just for IIS.; "only one context creates" not sure what you mean. Even when IsReusable is true, each call to ProcessRequest will receive a different HttpContext. IsReusable does not alter the way HTTP requests are processed and responses are sent. It has no effect on content whatsoever. – usr Jul 27 '13 at 13:26
  • When you send the request, this create a new object of a page in asp.net web pages(web pages are derived from `HttpHandler` and internally creates an instance of page and call the page events thereafter). I am going bit off topic. But trying to understand it fully...If this is true, then where did Context Switching occur ? For each Session, there is only one Context. Request is going to be created. So, when did it change the request for any intermediate jobs ? – Imad Alazani Jul 27 '13 at 13:41
  • A context switch has nothing to do with ASP.NET. If you disagree with this please say so.; With "Context" you probably mean HttpContext? That's different. We need to clean up the terminology first. Make sure you understand what both terms mean. "For each Session, there is only one Context" What exactly is meant by Context here? There is no relationship between an ASP.NET session and a context of any kind. "So, when did it change the request for any intermediate jobs ?" unclear! it? jobs? intermediate? I fail to interpret your sentence. – usr Jul 27 '13 at 14:36
  • Actually, I am not completely aware of what exactly is the meaning of Context switching? – Imad Alazani Jul 27 '13 at 14:44
  • That's when the OS removes one thread from the CPU and puts a different thread on it. That has little to do with ASP.NET. I can only encourage you to research those terms a bit. – usr Jul 27 '13 at 15:09
  • **That has little to do with ASP.NET.** That has nothing to do with MVC ? – Imad Alazani Jul 27 '13 at 15:11
  • If you just typed it into a search engine you would receive more information than I can add here. It has nothing to do with MVC. Do you have any other question that is directly related? – usr Jul 27 '13 at 15:17
  • No, i was just curious to know that. – Imad Alazani Jul 27 '13 at 15:18