0

No matter how nice and orderly my WCF service is about throwing exceptions:

throw new FaultException<SapphireServiceFault>(fault, new FaultReason(fault.Message))

They always wind up in the client handler as "400 - Bad Request". If you walk the chain of InnerException, it leads to one 400-BadRequest and then null. No mention of the FaultException.

My interface is decorated with the FaultContract attribute:

    [WebInvoke(Method = "POST", UriTemplate = "action/execute/{actionID}")]
    [OperationContract]
    [FaultContract(typeof(SapphireServiceFault))]
    void ExecuteActionGroup(string actionID);

The implementation of that interface just throws the Fault for testing:

    public void ExecuteActionGroup(string groupID)
    {
        SapphireServiceFault fault = new SapphireServiceFault("Bad Mojo");
        throw new FaultException<SapphireServiceFault>(fault, new FaultReason("Hello World"));
    ...
    }

In the client, I can even try to catch that specific exception, but it comes through at a 400, not a FaultException:

        try
        {
            Program.SapphireService.ExecuteActionGroup(actionGroup.ActionGroupName);
        }
        catch(FaultException<SapphireServiceFault> ex)
        {
            // Does not catch anything
            MessageBoxEx.Show("Failed to execute action: " + ex.Message);
        }

There must be something I'm overlooking!

As I am using programmatic binding I cannot add anything to the app.config, but anything that can be done there can usually be done in the bindings. For what it is worth, I have IncludeExceptionDetailInFaults turned on.

What am I missing to get more meaningful results back to the client?

Dave
  • 1,521
  • 17
  • 31
  • 1
    WCF debugging can be very tricky. I suggest you first verify that the 400 isn't caused by something else like e.g. a contract mismatch. You can use tracing (explained [here](https://stackoverflow.com/a/4271597/4684493), or alternatively tools like fiddler or wireshark to verify what is actually going over the line. – Hintham Jan 03 '18 at 18:17
  • @Dave what bindingConfiguration you are using ?Activate tracer and check whether the data that is thrown from service is more than buffer/specified memory size. – Hameed Syed Jan 04 '18 at 02:15
  • I am using WebHttpBinding straight out of the box except MaxReceivedMessageSize is set higher. – Dave Jan 04 '18 at 16:55

1 Answers1

0

It looks like you're trying to build RESTful service using WCF (hence usage of WebInvoke attribute).

You need to throw WebFaultException to get faults properly communicated over WebHttpBinding.

Igor Labutin
  • 1,406
  • 10
  • 10
  • I've tried that, but it still winds up on the client side as a BadProtocol exception. That'd be OK if I could get at the "additional" info I've passed in the WebFault (or better yet, a copy of my serialized fault object). Right now I can't find a way to get at any additional context in the receiver. – Dave Jan 10 '18 at 00:24
  • Could you show the code how you throw `WebFaultException`? Please also take a look at https://stackoverflow.com/questions/10797041/how-to-handle-webfaultexception-to-return-customexception to get examples of how to throw and consume it. – Igor Labutin Jan 11 '18 at 15:05