This documentation is archived and is not being maintained.

FaultException(Of TDetail) Class

Used in a client application to catch contractually-specified SOAP faults.

System.Object
  System.Exception
    System.SystemException
      System.ServiceModel.CommunicationException
        System.ServiceModel.FaultException
          System.ServiceModel.FaultException(Of TDetail)
            System.ServiceModel.Web.WebFaultException(Of T)

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

'Declaration
<SerializableAttribute> _
Public Class FaultException(Of TDetail) _
	Inherits FaultException

Type Parameters

TDetail

The serializable error detail type.

The FaultException(Of TDetail) type exposes the following members.

  NameDescription
Public methodFaultException(Of TDetail)(TDetail)Initializes a new instance of the FaultException(Of TDetail) class that uses the specified detail object.
Public methodFaultException(Of TDetail)(TDetail, FaultReason)Initializes a new instance of the FaultException(Of TDetail) class that uses the specified detail object and fault reason.
Public methodFaultException(Of TDetail)(TDetail, String)Initializes a new instance of the FaultException(Of TDetail) class that uses the specified detail and fault reason.
Protected methodFaultException(Of TDetail)(SerializationInfo, StreamingContext)Initializes a new instance of the FaultException(Of TDetail) class using the specified serialization information and context when deserializing a stream into a FaultException object.
Public methodFaultException(Of TDetail)(TDetail, FaultReason, FaultCode)Initializes a new instance of the FaultException(Of TDetail) class that uses the specified detail object, fault reason, and fault code.
Public methodFaultException(Of TDetail)(TDetail, String, FaultCode)Initializes a new instance of the FaultException(Of TDetail) class that uses the specified detail object, fault reason, and fault code.
Public methodSupported by Portable Class LibraryFaultException(Of TDetail)(TDetail, FaultReason, FaultCode, String)Initializes a new instance of the FaultException(Of TDetail) class that uses the specified detail object, and SOAP fault reason, code and action values.
Public methodFaultException(Of TDetail)(TDetail, String, FaultCode, String)Initializes a new instance of the FaultException(Of TDetail) class that uses the specified detail object, and SOAP fault reason, code and action values.
Top

  NameDescription
Public propertySupported by Portable Class LibraryActionGets the value of the SOAP action for the fault message. (Inherited from FaultException.)
Public propertySupported by Portable Class LibraryCodeGets the fault code for the SOAP fault. (Inherited from FaultException.)
Public propertyDataGets a collection of key/value pairs that provide additional user-defined information about the exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibraryDetailGets the object that contains the detail information of the fault condition.
Public propertyHelpLinkGets or sets a link to the help file associated with this exception. (Inherited from Exception.)
Protected propertySupported by Portable Class LibraryHResultGets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibraryInnerExceptionGets the Exception instance that caused the current exception. (Inherited from Exception.)
Public propertySupported by Portable Class LibraryMessageGets the message for the exception. (Inherited from FaultException.)
Public propertySupported by Portable Class LibraryReasonGets the FaultReason for the SOAP fault. (Inherited from FaultException.)
Public propertySourceGets or sets the name of the application or the object that causes the error. (Inherited from Exception.)
Public propertySupported by Portable Class LibraryStackTraceGets a string representation of the immediate frames on the call stack. (Inherited from Exception.)
Public propertyTargetSiteGets the method that throws the current exception. (Inherited from Exception.)
Top

  NameDescription
Public methodCreateMessageFaultCreates a MessageFault object that can be used to create a Message that represents the SOAP fault. (Overrides FaultException.CreateMessageFault.)
Public methodSupported by Portable Class LibraryEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Portable Class LibraryGetBaseExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception.)
Public methodSupported by Portable Class LibraryGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetObjectDataImplementation of the ISerializable.GetObjectData method that is called when the object is serialized into a stream. (Overrides FaultException.GetObjectData(SerializationInfo, StreamingContext).)
Public methodGetTypeGets the runtime type of the current instance. (Inherited from Exception.)
Protected methodSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Portable Class LibraryToStringReturns a string for the FaultException(Of TDetail) object. (Overrides Exception.ToString.)
Top

  NameDescription
Protected eventSerializeObjectStateOccurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Inherited from Exception.)
Top

Catch the FaultException(Of TDetail) object in a 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(Of 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(Of TDetail) is both a FaultException and therefore a CommunicationException, to catch specified SOAP faults make sure you catch the FaultException(Of TDetail) types prior to the FaultException and CommunicationException types or handle the specified exceptions in one of those exception handlers.

NoteNote

If you use System.ServiceModel.FaultContractAttribute to specify a FaultException(Of 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(Of 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. In general, it is recommended that detail types be custom serializable types appropriate to the fault and not a System.String.

The following code example shows how a service uses the FaultException(Of TDetail) type to throw a managed exception that gets converted into the SOAP fault specified by the FaultContractAttribute.



Imports System
Imports System.Collections.Generic
Imports System.Net.Security
Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.Text

Namespace Microsoft.WCF.Documentation
  <ServiceContract(Namespace:="http://microsoft.wcf.documentation")> _
  Public Interface ISampleService
	<OperationContract, FaultContractAttribute(GetType(GreetingFault), Action:="http://www.contoso.com/GreetingFault", ProtectionLevel:=ProtectionLevel.EncryptAndSign)> _
	Function SampleMethod(ByVal msg As String) As String
  End Interface

  <DataContractAttribute> _
  Public Class GreetingFault
	Private report As String

	Public Sub New(ByVal message As String)
	  Me.report = message
	End Sub

	<DataMemberAttribute> _
	Public Property Message() As String
	  Get
		  Return Me.report
	  End Get
	  Set(ByVal value As String)
		  Me.report = value
	  End Set
	End Property
  End Class

  Friend Class SampleService
	  Implements ISampleService
  #Region "ISampleService Members"

  Public Function SampleMethod(ByVal msg As String) As String Implements ISampleService.SampleMethod
	Console.WriteLine("Client said: " & msg)
	' Generate intermittent error behavior.
	Dim rand As New Random(DateTime.Now.Millisecond)
	Dim test As Integer = rand.Next(5)
	If test Mod 2 <> 0 Then
	  Return "The service greets you: " & msg
	Else
	  Throw New FaultException(Of GreetingFault)(New GreetingFault("A Greeting error occurred. You said: " & msg))
	End If
  End Function

  #End Region
  End Class
End Namespace


The following code example shows how the client code looks when imported by the client using the Service Model Metadata Utility Tool (Svcutil.exe).

The following code example shows how a client can catch the FaultException(Of TDetail) type that represents the custom SOAP fault specified in the operation contract.



Imports System
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports Microsoft.WCF.Documentation

Public Class Client
  Public Shared Sub Main()
	' Picks up configuration from the config file.
	Dim wcfClient As New SampleServiceClient()
	Try
	  ' Making calls.
	  Console.WriteLine("Enter the greeting to send: ")
	  Dim greeting As String = 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 timeProblem As TimeoutException
	  Console.WriteLine("The service operation timed out. " & timeProblem.Message)
	  Console.ReadLine()
	  wcfClient.Abort()
	Catch greetingFault As FaultException(Of GreetingFault)
	  Console.WriteLine(greetingFault.Detail.Message)
	  Console.ReadLine()
	  wcfClient.Abort()
	Catch unknownFault As FaultException
	  Console.WriteLine("An unknown exception was received. " & unknownFault.Message)
	  Console.ReadLine()
	  wcfClient.Abort()
	Catch commProblem As CommunicationException
	  Console.WriteLine("There was a communication problem. " & commProblem.Message + commProblem.StackTrace)
	  Console.ReadLine()
	  wcfClient.Abort()
	End Try
  End Sub
End Class


.NET Framework

Supported in: 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show: