55

I have the following code in WCF service to throw a custom fault based on certain situations. I am getting a "The creator of this fault did not specify a Reason" exception. What am I doing wrong?

//source code
if(!DidItPass)
{
    InvalidRoutingCodeFault fault = new InvalidRoutingCodeFault("Invalid Routing Code - No Approval Started");
    throw new FaultException<InvalidRoutingCodeFault>(fault);
}

//operation contract
[OperationContract]
[FaultContract(typeof(InvalidRoutingCodeFault))]
bool MyMethod();

//data contract
[DataContract(Namespace="http://myuri.org/Simple")]
public class InvalidRoutingCodeFault
{
    private string m_ErrorMessage = string.Empty;

    public InvalidRoutingCodeFault(string message)
    {
        this.m_ErrorMessage = message;
    }

    [DataMember]
    public string ErrorMessage
    {
        get { return this.m_ErrorMessage; }
        set { this.m_ErrorMessage = value; }
    }
}
3
  • 32
    You know, that message has some pretty profound philosophical implications outside of computer programming as well. Mar 4, 2010 at 12:51
  • i think i'll add that as my signature for some time.. thanks for that Michael
    – hangar18
    Jul 31, 2012 at 12:45
  • 1
    now looking at the original code sample in this question....(facepalm) Nov 14, 2013 at 15:09

10 Answers 10

50

After some addtional research, the following modified code worked:

if(!DidItPass)
{    
    InvalidRoutingCodeFault fault = new InvalidRoutingCodeFault("Invalid Routing Code - No Approval Started");    
    throw new FaultException<InvalidRoutingCodeFault>(fault, new FaultReason("Invalid Routing Code - No Approval Started"));
}
2
19

The short answer is you are doing nothing wrong, just reading the results incorrectly.

On the client side when you catch the error, what is caught is of the type System.ServiceModel.FaultException<InvalidRoutingCodeFault>.
Your InvalidRoutingCodeFault object is actually in the .detail property of the FaultException. SO....

// client code

private static void InvokeMyMethod() 
{ 
    ServiceClient service = new MyService.ServiceClient(); 

    try 
    { 
        service.MyMethod(); 
    } 
    catch (System.ServiceModel.FaultException<InvalidRoutingCodeFault> ex) 
    { 
        // This will output the "Message" property of the System.ServiceModel.FaultException
        // 'The creator of this fault did not specify a Reason' if not specified when thrown
        Console.WriteLine("faultException Message: " + ex.Message);    
        // This will output the ErrorMessage property of your InvalidRoutingCodeFault type
        Console.WriteLine("InvalidRoutingCodeFault Message: " + ex.Detail.ErrorMessage);    
    } 
}

The Message property of the FaultException is what is displayed on the error page so if it's not populated like in John Egerton's post, you will see the 'The creator of this fault did not specify a Reason' message. To easily populate it, use the two parameter constructor when throwing the fault in the service as follows, passing your error message from your fault type:

InvalidRoutingCodeFault fault = new InvalidRoutingCodeFault("Invalid Routing Code - No Approval Started");                                          
throw new FaultException<InvalidRoutingCodeFault>(fault, new FaultReason(fault.ErrorMessage));                                      
9
serviceDebug includeExceptionDetailInFaults="true"

is NOT the solution

The following code works even with serviceDebug includeExceptionDetailInFaults="false"

// data contract 

[DataContract]
public class FormatFault
{
    private string additionalDetails;

    [DataMember]
    public string AdditionalDetails
    {
        get { return additionalDetails; }
        set { additionalDetails = value; }
    }
}

// interface method declaration

    [OperationContract]
    [FaultContract(typeof(FormatFault))]
    void DoWork2();

// service method implementation

    public void DoWork2()
    {
        try
        {
            int i = int.Parse("Abcd");
        }
        catch (FormatException ex)
        {
            FormatFault fault = new FormatFault();
            fault.AdditionalDetails = ex.Message;
            throw new FaultException<FormatFault>(fault);
        }
    }

// client calling code

    private static void InvokeWCF2()
    {
        ServiceClient service = new ServiceClient();

        try
        {
            service.DoWork2();
        }
        catch (FaultException<FormatFault> e)
        {
            // This is a strongly typed try catch instead of the weakly typed where we need to do -- if (e.Code.Name == "Format_Error")
            Console.WriteLine("Handling format exception: " + e.Detail.AdditionalDetails);   
        }
    }

There is no need to add fault reason if its not required. Just make sure the FaultContract attribute is correct

1
  • The e.Detail portion is key. This is the answer for the OP's question. Aug 1, 2012 at 15:35
5

I solved this problem using a two parameter constructer.

// service method implementation

 throw new FaultException<FormatFault>(fault,new FaultReason(fault.CustomFaultMassage)); 

CustomFaultMassage is property from data contract.

2

One can also encounter this exception if one does not specify the FaultContract(typeof(className)) attribute for the method

2

If you do not want to be notified of such exceptions go to Debug -> Exceptions and uncheck "User-unhandled" for "Common Language Runtime Exceptions" or for specific exceptions.

1

I have code exactly like Rashmi has and I got the "The creator of this fault...." error. It was happening when I was debugging in VS2010. I found this post:

http://sergecalderara.wordpress.com/2008/11/25/systemservicemodelfaultexception1-was-unhandled-by-user-code/

which explained a couple of debugging options that I needed to turn off. Problem solved.

0

You might try this in the server config (behaviors -> serviceBehaviors -> behavior):

<serviceDebug includeExceptionDetailInFaults="true" />
3
  • I have that set to true in my config file. Dec 4, 2008 at 19:04
  • Ok, I thought it was likely a more complicated issue than that, but I always want to check the basics. I'm glad you found a solution!! Dec 4, 2008 at 19:27
  • Please note that on msdn they recommend to use this setting only when debugging and not on production.
    – ilansch
    Mar 28, 2013 at 6:50
0

By using strongly typed try catch, I was able get around with the error "The creator of this fault did not specify a Reason".

0

Updating the service reference in the client solved the problem. Same could work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.