FaultException<TDetail> Class
Used in a client application to catch contractually-specified SOAP faults.
Assembly: System.ServiceModel (in System.ServiceModel.dll)
System.Exception
System.SystemException
System.ServiceModel.CommunicationException
System.ServiceModel.FaultException
System.ServiceModel.FaultException<TDetail>
System.ServiceModel.Web.WebFaultException<T>
| Name | Description | |
|---|---|---|
![]() | FaultException<TDetail>(TDetail) | Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object. |
![]() | FaultException<TDetail>(TDetail, FaultReason) | Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object and fault reason. |
![]() | FaultException<TDetail>(TDetail, FaultReason, FaultCode) | Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object, fault reason, and fault code. |
![]() | FaultException<TDetail>(TDetail, FaultReason, FaultCode, String) | Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object, and SOAP fault reason, code and action values. |
![]() | FaultException<TDetail>(TDetail, String) | Initializes a new instance of the FaultException<TDetail> class that uses the specified detail and fault reason. |
![]() | FaultException<TDetail>(TDetail, String, FaultCode) | Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object, fault reason, and fault code. |
![]() | FaultException<TDetail>(TDetail, String, FaultCode, String) | Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object, and SOAP fault reason, code and action values. |
![]() | FaultException<TDetail>(SerializationInfo, StreamingContext) | Initializes a new instance of the FaultException<TDetail> class using the specified serialization information and context when deserializing a stream into a FaultException object. |
| Name | Description | |
|---|---|---|
![]() | Action | Gets the value of the SOAP action for the fault message.(Inherited from FaultException.) |
![]() | Code | Gets the fault code for the SOAP fault.(Inherited from FaultException.) |
![]() | Data | Gets a collection of key/value pairs that provide additional user-defined information about the exception.(Inherited from Exception.) |
![]() | Detail | Gets the object that contains the detail information of the fault condition. |
![]() | HelpLink | Gets or sets a link to the help file associated with this exception.(Inherited from Exception.) |
![]() | HResult | Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.(Inherited from Exception.) |
![]() | InnerException | |
![]() | Message | Gets the message for the exception.(Inherited from FaultException.) |
![]() | Reason | Gets the FaultReason for the SOAP fault.(Inherited from FaultException.) |
![]() | Source | Gets or sets the name of the application or the object that causes the error.(Inherited from Exception.) |
![]() | StackTrace | Gets a string representation of the immediate frames on the call stack.(Inherited from Exception.) |
![]() | TargetSite | Gets the method that throws the current exception.(Inherited from Exception.) |
| Name | Description | |
|---|---|---|
![]() | CreateMessageFault() | Creates a MessageFault object that can be used to create a Message that represents the SOAP fault.(Overrides FaultException.CreateMessageFault().) |
![]() | Equals(Object) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetBaseException() | |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetObjectData(SerializationInfo, StreamingContext) | Implementation of the ISerializable.GetObjectData method that is called when the object is serialized into a stream.(Overrides FaultException.GetObjectData(SerializationInfo, StreamingContext).) |
![]() | GetType() | Gets the runtime type of the current instance.(Inherited from Exception.) |
![]() | MemberwiseClone() | |
![]() | ToString() | Returns a string for the FaultException<TDetail> object.(Overrides Exception.ToString().) |
| Name | Description | |
|---|---|---|
![]() | SerializeObjectState | Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.(Inherited from Exception.) |
Catch the FaultException<TDetail> object in a Windows Communication Foundation (WCF) client application to handle a SOAP fault that has been contractually specified in an operation contract.
Typical deployed services use the FaultContractAttribute to formally specify all SOAP faults that a client can expect to receive in the normal course of an operation. Error information in a FaultContractAttribute appears as a FaultException<TDetail> (where the typeparameter is the serializable error object specified in the operation's FaultContractAttribute) when it arrives at a client application. The FaultContractAttribute can be used to specify SOAP faults for both two-way service methods and for asynchronous method pairs.
Because FaultException<TDetail> is both a FaultException and therefore a CommunicationException, to catch specified SOAP faults make sure you catch the FaultException<TDetail> types prior to the FaultException and CommunicationException types or handle the specified exceptions in one of those exception handlers.
Note |
|---|
If you use System.ServiceModel.FaultContractAttribute to specify a FaultException<TDetail> where the type parameter is a System.String, the string value is assigned to the Detail property in the client application; clients cannot retrieve that string by calling the FaultException<TDetail>.ToString method. To have the string value returned when the client application calls Exception.ToString, throw a System.ServiceModel.FaultException exception inside the operation and pass the string to the constructor. |
The following code example shows how a service uses the FaultException<TDetail> type to throw a managed exception that gets converted into the SOAP fault specified by the FaultContractAttribute.
using System; using System.Collections.Generic; using System.Net.Security; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace Microsoft.WCF.Documentation { [ServiceContract(Namespace="http://microsoft.wcf.documentation")] public interface ISampleService{ [OperationContract] [FaultContractAttribute( typeof(GreetingFault), Action="http://www.contoso.com/GreetingFault", ProtectionLevel=ProtectionLevel.EncryptAndSign )] string SampleMethod(string msg); } [DataContractAttribute] public class GreetingFault { private string report; public GreetingFault(string message) { this.report = message; } [DataMemberAttribute] public string Message { get { return this.report; } set { this.report = value; } } } class SampleService : ISampleService { #region ISampleService Members public string SampleMethod(string msg) { Console.WriteLine("Client said: " + msg); // Generate intermittent error behavior. Random rnd = new Random(DateTime.Now.Millisecond); int test = rnd.Next(5); if (test % 2 != 0) return "The service greets you: " + msg; else throw new FaultException<GreetingFault>(new GreetingFault("A Greeting error occurred. You said: " + msg)); } #endregion } }
The following code example shows how the client code looks when imported by the client using the ServiceModel Metadata Utility Tool (Svcutil.exe).
The following code example shows how a client can catch the FaultException<TDetail> type that represents the custom SOAP fault specified in the operation contract.
using System; using System.ServiceModel; using System.ServiceModel.Channels; using Microsoft.WCF.Documentation; public class Client { public static void Main() { // Picks up configuration from the config file. SampleServiceClient wcfClient = new SampleServiceClient(); try { // Making calls. Console.WriteLine("Enter the greeting to send: "); string greeting = Console.ReadLine(); Console.WriteLine("The service responded: " + wcfClient.SampleMethod(greeting)); Console.WriteLine("Press ENTER to exit:"); Console.ReadLine(); // Done with service. wcfClient.Close(); Console.WriteLine("Done!"); } catch (TimeoutException timeProblem) { Console.WriteLine("The service operation timed out. " + timeProblem.Message); Console.ReadLine(); wcfClient.Abort(); } catch (FaultException<GreetingFault> greetingFault) { Console.WriteLine(greetingFault.Detail.Message); Console.ReadLine(); wcfClient.Abort(); } catch (FaultException unknownFault) { Console.WriteLine("An unknown exception was received. " + unknownFault.Message); Console.ReadLine(); wcfClient.Abort(); } catch (CommunicationException commProblem) { Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace); Console.ReadLine(); wcfClient.Abort(); } } }
Available since 8
.NET Framework
Available since 3.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 3.0
Windows Phone Silverlight
Available since 7.0
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.




