"The creator of this fault did not specify a Reason" Exception

C#.NetWcfFaults

C# Problem Overview


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; }
    }
}

C# Solutions


Solution 1 - C#

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"));
}

Solution 2 - C#

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));                                      
                              

Solution 3 - C#

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

Solution 4 - C#

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.

Solution 5 - C#

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

Solution 6 - C#

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.

Solution 7 - C#

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.

Solution 8 - C#

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

<serviceDebug includeExceptionDetailInFaults="true" />

Solution 9 - C#

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

Solution 10 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael KniskernView Question on Stackoverflow
Solution 1 - C#Michael KniskernView Answer on Stackoverflow
Solution 2 - C#Daniel DavisView Answer on Stackoverflow
Solution 3 - C#SO UserView Answer on Stackoverflow
Solution 4 - C#user386451View Answer on Stackoverflow
Solution 5 - C#SO UserView Answer on Stackoverflow
Solution 6 - C#sandeep patilView Answer on Stackoverflow
Solution 7 - C#ScottGView Answer on Stackoverflow
Solution 8 - C#Chris PorterView Answer on Stackoverflow
Solution 9 - C#UJ View Answer on Stackoverflow
Solution 10 - C#pencilslateView Answer on Stackoverflow