Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throwing a FaultException<T> from a WCF services causes 'The creator of this fault did not specify a Reason.'

When throwing a FaultException<CustomFault> like this:

throw new FaultException<CustomFault>(new CustomFault("Custom fault message"));

I get the following: "The creator of this fault did not specify a Reason." Now according to this MSDN article one isn't required to use the FaultReason

I have the following Service Contract:

[OperationContract]
[FaultContract(typeof(CustomFault))]
CustomType[] SomeMethod(int someParameter);

[DataContract]
public class CustomFault
{
    private string report;

    public CustomFault(string message)
    {
        this.report = message;
    }

    [DataMember]
    public string Message
    {
        get { return this.report; }
        set { this.report = value; }
    }
}

There is a comment on the MSDN article that suggest it is mandatory to use the other FaultReason, but in several other places I have seen people arguing that you shouldn't use the FaultReason unless necessary.

So my question is as follows; is it really mandatory to use the FaultReason and if not - how can i prevent the exception raised when trying to throw a FaultException?

EDIT

By running the sample project from this article I get the excact same behavior. Maybe it's caused by an update in .NET an the documentation/samples not being updated.

like image 560
Nicklas Møller Jepsen Avatar asked Mar 08 '12 08:03

Nicklas Møller Jepsen


People also ask

Why We Need fault contracts in WCF why not just use error codes?

A Fault Contract is a way to handle an error/exception in WCF. In C# we can handle the error using try and catch blocks at the client-side. The purpose of a Fault Contract is to handle an error by the service class and display in the client-side.

What is fault exception in WCF?

Fault exception in WCF. It is used in a client application to catch contractually-specified SOAP faults. By the simple exception message, you can't identify the reason of the exception, that's why a Fault Exception is useful.

Which exception type is thrown by all WCF component methods?

Retrace can automatically collect all . NET exceptions that are occurring within your application with no code changes. This includes unhandled exceptions in WCF but can also include all thrown exceptions, or first chance exceptions.

How do you handle a fault exception in C#?

In a service, use the FaultException class to create an untyped fault to return to the client for debugging purposes. In a client, catch FaultException objects to handle unknown or generic faults, such as those returned by a service with the IncludeExceptionDetailInFaults property set to true .


1 Answers

The fault reason is required while the details are optional.

WCF uses the value of the FaultException.Reason property to populate the <faultstring> element of the SOAP Fault, which is required by the SOAP specification:

faultstring
The faultstring element is intended to provide a human readable explanation of the fault and is not intended for algorithmic processing. [...] It MUST be present in a SOAP Fault element and SHOULD provide at least some information explaining the nature of the fault.

Hence, you must provide a reason for the fault when throwing a FaultException from a WCF service.

like image 57
Enrico Campidoglio Avatar answered Sep 23 '22 10:09

Enrico Campidoglio