FaultException Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Used in a client application to catch contractually-specified SOAP faults.
generic
public ref class FaultException : System::ServiceModel::FaultException
public class FaultException : System.ServiceModel.FaultException
[System.Serializable]
public class FaultException : System.ServiceModel.FaultException
type FaultException<'Detail> = class
inherit FaultException
[]
type FaultException<'Detail> = class
inherit FaultException
Public Class FaultException(Of TDetail)
Inherits FaultException
Type Parameters
- TDetail
The serializable error detail type.
- Inheritance
- Inheritance
- Derived
- Attributes
Examples
The following code example shows how a service uses the FaultException
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(new GreetingFault("A Greeting error occurred. You said: " + msg));
}
#endregion
}
}
Imports System.Collections.Generic
Imports System.Net.Security
Imports System.Runtime.Serialization
Imports System.ServiceModel
Imports System.Text
Namespace Microsoft.WCF.Documentation
_
Public Interface ISampleService
_
Function SampleMethod(ByVal msg As String) As String
End Interface
_
Public Class GreetingFault
Private report As String
Public Sub New(ByVal message As String)
Me.report = message
End Sub
_
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 ServiceModel Metadata Utility Tool (Svcutil.exe).
The following code example shows how a client can catch the FaultException
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)
{
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();
}
}
}
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
Remarks
Catch the FaultException
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
Because FaultException
Note
If you use System.ServiceModel.FaultContractAttribute to specify a FaultException
Constructors
| Name | Description |
|---|---|
|
FaultException |
Initializes a new instance of the FaultException |
|
FaultException |
Initializes a new instance of the FaultException |
|
FaultException |
Initializes a new instance of the FaultException |
|
FaultException |
Initializes a new instance of the FaultException |
|
FaultException |
Initializes a new instance of the FaultException |
|
FaultException |
Initializes a new instance of the FaultException |
|
FaultException |
Initializes a new instance of the FaultException |
|
FaultException |
Initializes a new instance of the FaultException |
Properties
| 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 |
Gets the Exception instance that caused the current exception. (Inherited from Exception) |
| 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) |
Methods
| Name | Description |
|---|---|
| CreateMessageFault() |
Creates a MessageFault object that can be used to create a Message that represents the SOAP fault. |
| Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
| GetBaseException() |
When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (Inherited from Exception) |
| GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
Implementation of the GetObjectData(SerializationInfo, StreamingContext) method that is called when the object is serialized into a stream. |
| GetType() |
Gets the runtime type of the current instance. (Inherited from Exception) |
| MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
| ToString() |
Returns a string for the FaultException |
Events
| Name | Description |
|---|---|
| SerializeObjectState |
Obsolete.
Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (Inherited from Exception) |