Mark an operation with the FaultContractAttribute attribute to declare one or more specific exception conditions that are added to the Web Service Description Language (WSDL) description of the service operation as explicit SOAP fault messages returned by the operation.
In all managed applications, processing errors are represented by Exception objects. In SOAP-based applications such as Windows Communication Foundation (WCF) applications, service methods communicate processing error information using SOAP fault messages. Because WCF applications execute under both types of error systems, any managed exception information that must be sent to the client must be converted from exceptions into SOAP faults. You can use the default service exception behaviors, or you can explicitly control whether -- and how -- exceptions are mapped to fault messages. For an overview of exceptions and SOAP faults in WCF applications, see Specifying and Handling Faults in Contracts and Services.
It is recommended that service operations use the FaultContractAttribute to formally specify all SOAP faults that a client can expect to receive in the normal course of an operation. It is also recommended that only that information a client must know is returned in a SOAP fault to minimize information disclosure.
The Action property controls the action of the fault message.
The DetailType property gets the type of the detail object serialized in the fault message.
The Name and Namespace properties control the name and namespace, respectively, of the fault message.
The HasProtectionLevel indicates whether the fault message has a protection level specified, and if so, the ProtectionLevel property controls that level of protection.
Caution: |
|---|
If a fault message carries information that is sensitive or can lead to security problems, it is strongly recommended that the ProtectionLevel property be set. |
For many scenarios setting ProtectionLevel to EncryptAndSign for fault messages is sufficient. For more details, see Understanding Protection Level.
To return a specified fault from an operation marked with FaultContractAttribute, throw a FaultException<(Of <(TDetail>)>) (where the type parameter is the serializable error information) when the managed exception occurs during the operation. WCF client applications surface the SOAP fault as the same type as was thrown in the client implementation -- that is, as a FaultException<(Of <(TDetail>)>) (where the typeparameter is the serializable error information). The FaultContractAttribute can be used only to specify SOAP faults for two-way service operations and for asynchronous operation pairs; one-way operations do not support SOAP faults and therefore do not support FaultContractAttribute.
For example, to specify that clients can expect a SOAP fault that contains an Int32, place that type parameter in the FaultContractAttribute on your service method.
<OperationContractAttribute(), FaultContractAttribute(GetType(Integer))> _
Function Divide(ByVal arg1 As Integer, ByVal arg2 As Integer) As Integer
End Interface 'FCADemonstration
[OperationContractAttribute]
[FaultContractAttribute(typeof(int))]
int Divide(int arg1, int arg2);
Then, in your service method, throw a new FaultException<(Of <(TDetail>)>) where the type parameter is the type that contains the error information (in the above case, a Int32). For example:
Throw New FaultException(Of Integer)(4)
throw new FaultException<int>(4);
The preceding example is very basic; almost any information can be passed using an System..::.Int32 code, so this detail type is not the most useful. Typically, WCF applications specify SOAP faults with detail types specific to the error information requirements of the client. For a more complete example, see the Example section.
To explicitly control the behavior of the application when an exception or FaultException<(Of <(TDetail>)>) is thrown, implement the System.ServiceModel.Dispatcher..::.IErrorHandler interface on an System.ServiceModel.Description..::.IServiceBehavior, System.ServiceModel.Description..::.IContractBehavior or System.ServiceModel.Description..::.IEndpointBehavior and assign it to the ChannelDispatcherErrorhandlers()()() property. IErrorHandlerenables you to explicitly control the SOAP fault that is generated and whether to send it back to the client.
To facilitate debugging, set the ServiceBehaviorAttribute..::.IncludeExceptionDetailInFaults to true in code or you can use the ServiceDebugBehavior..::.IncludeExceptionDetailInFaults in an application configuration file. When enabled, the service automatically returns exception information to the caller. These faults appear to the client as FaultException exceptions.