tags:

views:

5870

answers:

10

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(!DidNotPass)
{
    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; }
    }
}
A: 

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

<serviceDebug includeExceptionDetailInFaults="true" />
Chris Porter
I have that set to true in my config file.
Michael Kniskern
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!!
Chris Porter
+7  A: 

After some addtional research, the following modified code worked:

if(!DidNotPass)
{    
    InvalidRoutingCodeFault fault = new InvalidRoutingCodeFault("Invalid Routing Code - No Approval Started");    
    throw new FaultException<InvalidRoutingCodeFault>(fault, new FaultReason("Invalid Routing Code - No Approval Started"));
}
Michael Kniskern
Isn't it because your fault doesn't inherit from Exception?
SLC
A: 

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

Rashmi Pandit
+3  A: 

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

Rashmi Pandit
A: 

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

A: 

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.

Scott
A: 

You can also get this error. if the service is encountering an unexpected error other than the Faults that one has mentioned in the Service Contracts. A generic Fault contract of Type Service Fault can handle this.

Dinesh Menon
A: 

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.

sandeep patil
A: 

I solved this problem using a two parameter constructer.

// service method implementation

throw new FaultException(fault,new FaultReason(fault.CustomFaultMassage));

CustomFaultMassage is property from data contract.

A: 

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

pencilslate