title: WCF Rest Error Handling
body: <p>I'm having a mind blowing problem using WCF 4.0 RESTful  service. I am trying to make  a rest service that will return, in case of an error, a xml document  describing the problem
  ex :</p>
  
  <pre><code>    &lt;ErrorHandler&gt;
          &lt;cause&gt;Resource not  available&lt;/cause&gt;
          &lt;errorCode&gt;111103&lt;/errorCode&gt;
      &lt;/ErrorHandler&gt;
  </code></pre>
  
  <p>In order to make this i've created a default REST service using the  template provided by visual studio 
  Here  is my service  Class :</p>
  
  <pre><code>public class Service1
      {
          // TODO: Implement the collection resource that will contain the SampleItem instances
  
          [WebGet(UriTemplate = "")]        
          public List&lt;SampleItem&gt; GetCollection()
          {
              // TODO: Replace the current implementation to return a collection of SampleItem instances\
             // throw new WebException("lala");
             throw new WebFaultException&lt;ErrorHandler&gt;(new ErrorHandler { cause = "Resource not  available", errorCode = 100 }, System.Net.HttpStatusCode.NotFound);
              //return new List&lt;SampleItem&gt;() { new SampleItem() { Id = 1, StringValue = "Hello" } };
          }
  
          [WebInvoke(UriTemplate = "", Method = "POST")]
          public SampleItem Create(SampleItem instance)
          {
              // TODO: Add the new instance of SampleItem to the collection           
              return new SampleItem() { Id = 3, StringValue = "59" };
  
          }
  
          [WebGet(UriTemplate = "{id}")]
          public SampleItem Get(string id)
          {
              // TODO: Return the instance of SampleItem with the given id
              throw new NotImplementedException();
          }
  
          [WebInvoke(UriTemplate = "{id}", Method = "PUT")]
          public SampleItem Update(string id, SampleItem instance)
          {
              // TODO: Update the given instance of SampleItem in the collection
              throw new NotImplementedException();
          }
  
          [WebInvoke(UriTemplate = "{id}", Method = "DELETE")]
          public void Delete(string id)
          {
              // TODO: Remove the instance of SampleItem with the given id from the collection
              throw new NotImplementedException();
          }
  
      }
  }
  </code></pre>
  
  <p>As you can see from the code above i am throwing a WebFaultException in the  GetCollection method. that should put in the body of  the response a "ErrorHandler"  object. 
  Here is how my ErrorHandler class looks like :</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Runtime.Serialization;
  
  namespace WcfRestService1
  {
      [DataContract]
      public class ErrorHandler
      {
          [DataMember]
          public int errorCode { get; set; }
          [DataMember]
          public string cause { get; set; }
  
      }
  }
  </code></pre>
  
  <p>The crazy thing is that this thing works but it down't :)). What i'm trying to say is that  visual studio is   giving me an error saying that the WebFaultException is not caught by the user code :| and it suspends my app. If i  press continue   everything works as it should. 
  Here are some pictures describing my problem:</p>
  
  <p>First step in fiddler :
  <a href="http://i130.photobucket.com/albums/p245/dreamer_prime/Step1.png" rel="nofollow">First Step</a></p>
  
  <p>Next  Visual Studio's error: <a href="http://i130.photobucket.com/albums/p245/dreamer_prime/Error.png" rel="nofollow">Visual Studio Error</a></p>
  
  <p>Finally After  pressing continue   everything  works :</p>
  
  <p>It makes no sense to me  and i have no idea why this thing is happening and  how to fix it :P. I've searched the web  for days  trying to find a solution  with no luck.  I'm using Visual Studio 2010 Ultimate</p>
  
  <p>Best Regards  :)</p>