Catching WebFaultException RRS feed

  • Question

  • Hello,

    I've run into a bit of a problem using WebFaultException<>. There are many blog posts all over the Internet, as well as a modicum of Microsoft documentation, on how to *throw* this exception in a WCF/REST web service setting, but absolutely nothing that demonstrates how to successfully catch it and use its contents. Granted, a lot is to be found on using the predecessor technology of FaultException's -- but that is still not the same thing.

    Long story short, here's what I throw:

    throw new WebFaultException<string>( "test", HttpStatusCode.BadRequest );

    When I fire Fiddler up and look at the response content, it seems indeed to be just what I expect:

    <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">test</string>

    However, on the receiving (i.e. client) end (that extends a ClientBase<TChannel>), I can never catch this web fault with the straightforward:

    catch ( WebFaultException<string> webFaultException ) { ... }

    Instead, the exception I do see raised on the client end is of the type "ProtocolException" whose message reads: "The remote server returned an unexpected response: (400) Bad Request."

    I've played with other HTTP response codes as well, and here is what I get in those cases:

    • For code InternalServerError, 500: CommunicationException, "Internal Server Error"
    • For code ExpectationFailed, 417: ProtocolException, "The remote server returned an unexpected response: (417) Expectation Failed."
    • For code OK, 200: InvalidCastException, "Return argument has an invalid type."

    None of these exceptions are obviously of the desired type WebFaultException and there is no way for me to get at the string "test" that I embed in the WebFaultException that I throw...

    Hence my question: has anyone suceeded in actually *fully using* this new type of exception in practice (as opposed to hands-off blogging about it having been released) and has actual working code to demonstrate the process?

    I'm also rather disappointed by how little (working-example) documentation Microsoft has so far provided on using this "latest and greatest" WebFaultException technology...

    Anyway, that's the rant; thanks in advance for any constructive input! : )

    D. P.

    • Edited by QuestionAsker2010 Thursday, May 20, 2010 5:40 PM Made it prettier on the eyes
    Thursday, May 20, 2010 5:38 PM

All replies

  • I would greatly appreciate it if someone could provide a reply today -- hopefully the people at Microsoft, who know all the ins and outs :)


    D. P.

    Friday, May 21, 2010 8:55 AM
  • Hello, all,

    Just want to signal that I am still very much interested in some expert feedback on this item (preferably from the dev. team at Microsoft that designed this feature). I need to soon make a call on whether WebFaultExceptions have the potential to give us (in Restful WCF services) what FaultExceptions provide for SOAP services -- i.e. an ability to throw/catch things transparently, without needing to "manually" look into HTTP responses and/or deserialize the contents thereof.

    In other words, is it at all possible to get something like a "catch (WebFaultException e)" to work under my setup?

    Many thanks!

    D. P.

    Tuesday, May 25, 2010 8:22 AM