Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
FaultException<(Of <(TDetail>)>) Class

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

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
Visual Basic (Declaration)
<SerializableAttribute> _
Public Class FaultException(Of TDetail) _
    Inherits FaultException
Visual Basic (Usage)
Dim instance As FaultException(Of TDetail)
C#
[SerializableAttribute]
public class FaultException<TDetail> : FaultException
Visual C++
[SerializableAttribute]
generic<typename TDetail>
public ref class FaultException : public FaultException
JScript
JScript does not support generic types or methods.

Type Parameters

TDetail

The serializable error detail type.

Catch the FaultException<(Of <(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<(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.

C#
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 rand = new Random(DateTime.Now.Millisecond);
    int test = rand.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 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.

C#
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();
    }
  }
}

System..::.Object
  System..::.Exception
    System..::.SystemException
      System.ServiceModel..::.CommunicationException
        System.ServiceModel..::.FaultException
          System.ServiceModel..::.FaultException<(Of <(TDetail>)>)
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

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

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
FaultException requires a reason      SondreB ... weisjohn   |   Edit   |   Show History

When you create a new FaultException, you need to supply a reason (either using the string overload or the FaultReason type overload). When you fail to supply the reason, you can receive the following exception:

The creator of this fault did not specify a Reason.

Above example should be changed into the following code:

throw new FaultException<GreetingFault>(new GreetingFault("A Greeting error occurred. You said: " + msg), "A Greeting error occurred.");

FaultException DOES NOT requires a reason Ignore Above      Madu Alikor   |   Edit   |   Show History
Tags What's this?: Add a tag
Flag as ContentBug
Best practice for handling FaultException      Jesper Kihlberg   |   Edit   |   Show History
Is it really best practice to call client.Abort() when catching FaultException. I have experienced timeouts on subsequent service calls when a total of 10 clients were aborted with the error:
"The request channel timed out while waiting for a reply after 00:01:00. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout."
It seems like the client.Abort() call clogs up the channels.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker