none
How to deal with WebFaultException correctly? RRS feed

  • Question

  • Hi,

    I have a WCF REST service which throws some WebFaultException<string> (it seems to be the best practice to manage exception in a REST service...).

    For example : 

    throw new WebFaultException<string>("Ooops!", System.Net.HttpStatusCode.BadRequest);
    

    I also add this attribute to my operation :

    [OperationContract]
    [FaultContract(typeof(string))]
    string Test(string a, string b);
    

     

    But client side, if I do a catch(WebFaultException<string> e), it never handles this exception.

    My webFaultException server side seemed to be converted into a FaultException<string> client side .... 

    Is it possible to keep the WebFaultExcpetion client side? Because I lost le httpStatusCode which was set in the WebFaultException.

     

    Thank you

    Tuesday, July 19, 2011 1:51 PM

Answers

  • WCF clients don't handle web faults too good - you will get an exception of different type, such as a general fault exception or an endpoint not found exception (depends on the http return code).

    If you want to use web faults, I suggest you download the WCF REST starter kit - although it is for version 3.5 and wasn't released officially, it will give you the good client utilities to call restful services and handle their web faults.


    Please mark posts as answers/helpful if it answers your question. Senior Consultant on WCF, ASP.NET, Siverlight, and Entity Framework. Author of Microsoft's Official WCF 4 Course. Co-author of the Microsoft HPC/Azure burst whitepaper. Visit my blog: http://blogs.microsoft.co.il/blogs/idof
    • Proposed as answer by Ido Flatow. _ Wednesday, July 20, 2011 9:59 AM
    • Marked as answer by Yi-Lun Luo Monday, July 25, 2011 8:57 AM
    Tuesday, July 19, 2011 3:24 PM
  • The client utility from the REST Starter Kit (the HttpClient class and its friends) can now be downloaded along with the WCF Web APIs from codeplex at http://wcf.codeplex.com.

     


    Carlos Figueira
    • Marked as answer by Yi-Lun Luo Monday, July 25, 2011 8:57 AM
    Tuesday, July 19, 2011 4:31 PM
    Moderator

All replies

  • WCF clients don't handle web faults too good - you will get an exception of different type, such as a general fault exception or an endpoint not found exception (depends on the http return code).

    If you want to use web faults, I suggest you download the WCF REST starter kit - although it is for version 3.5 and wasn't released officially, it will give you the good client utilities to call restful services and handle their web faults.


    Please mark posts as answers/helpful if it answers your question. Senior Consultant on WCF, ASP.NET, Siverlight, and Entity Framework. Author of Microsoft's Official WCF 4 Course. Co-author of the Microsoft HPC/Azure burst whitepaper. Visit my blog: http://blogs.microsoft.co.il/blogs/idof
    • Proposed as answer by Ido Flatow. _ Wednesday, July 20, 2011 9:59 AM
    • Marked as answer by Yi-Lun Luo Monday, July 25, 2011 8:57 AM
    Tuesday, July 19, 2011 3:24 PM
  • The client utility from the REST Starter Kit (the HttpClient class and its friends) can now be downloaded along with the WCF Web APIs from codeplex at http://wcf.codeplex.com.

     


    Carlos Figueira
    • Marked as answer by Yi-Lun Luo Monday, July 25, 2011 8:57 AM
    Tuesday, July 19, 2011 4:31 PM
    Moderator