title: Exception Handling in REST
body: <p>I have created a REST service using WCF for communicating with BLL/DAL from UI. Now, I would like to implement Exception Handling through some globalize way in REST. I am using HTTPClient to communicate with REST from client. The following are the ways, I have implemented exception handling: </p>
  
  <p>Approach 1:</p>
  
  <pre><code>OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse;
  response.StatusCode = System.Net.HttpStatusCode.Unauthorized;
  response.StatusDescription = ex.Message;
  success = false;
  </code></pre>
  
  <p>Approach 2: </p>
  
  <pre><code>throw new WebProtocolException(HttpStatusCode.NotFound, ex.Message, null);
  </code></pre>
  
  <p>but somehow I am not able to catch them in the client side. Kindly suggest some pointers in order to handle exceptions raised by service globally.</p>
  
  <hr>
  
  <p>I am throwing WebProtocolException (Approach 2) along with error code and error message from the service to UI in order to notify users about the exception. I am able to receive the error code in UI side but not getting error message or detail.</p>
  
  <p>Then I created another service (having .svc extension) and added following line to the service's markup: "Factory='Microsoft.ServiceModel.Web.WebServiceHost2Factory'". Now, I am able to receive the error message along with error code on client side (UI). </p>
  
  <p>Can anybody let me know how to use the WebServiceHost2Factory method in my existing service which is a class file instead of .svc file?</p>
  
  <p>Any suggestions will be highly appreciated. Thanks</p>