FaultException 类

定义

表示 SOAP 错误。

public ref class FaultException : System::ServiceModel::CommunicationException
public class FaultException : System.ServiceModel.CommunicationException
[System.Serializable]
public class FaultException : System.ServiceModel.CommunicationException
[System.Serializable]
[System.Runtime.Serialization.KnownType(typeof(System.ServiceModel.FaultException+FaultCodeData[]))]
[System.Runtime.Serialization.KnownType(typeof(System.ServiceModel.FaultException+FaultReasonData[]))]
[System.Runtime.Serialization.KnownType(typeof(System.ServiceModel.FaultException+FaultCodeData))]
[System.Runtime.Serialization.KnownType(typeof(System.ServiceModel.FaultException+FaultReasonData))]
public class FaultException : System.ServiceModel.CommunicationException
type FaultException = class
    inherit CommunicationException
[<System.Serializable>]
type FaultException = class
    inherit CommunicationException
[<System.Serializable>]
[<System.Runtime.Serialization.KnownType(typeof(System.ServiceModel.FaultException+FaultCodeData[]))>]
[<System.Runtime.Serialization.KnownType(typeof(System.ServiceModel.FaultException+FaultReasonData[]))>]
[<System.Runtime.Serialization.KnownType(typeof(System.ServiceModel.FaultException+FaultCodeData))>]
[<System.Runtime.Serialization.KnownType(typeof(System.ServiceModel.FaultException+FaultReasonData))>]
type FaultException = class
    inherit CommunicationException
Public Class FaultException
Inherits CommunicationException
继承
继承
派生
属性

示例

下面的代码示例演示使用 try/catch 块捕获和处理从服务引发的 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 configuration 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();
    }
    catch (TimeoutException timeProblem)
    {
      Console.WriteLine("The service operation timed out. " + timeProblem.Message);
      wcfClient.Abort();
      Console.ReadLine();
    }
    // Catch the contractually specified SOAP fault raised here as an exception.
    catch (FaultException<GreetingFault> greetingFault)
    {
      Console.WriteLine(greetingFault.Detail.Message);
      Console.Read();
      wcfClient.Abort();
    }
    // Catch unrecognized faults. This handler receives exceptions thrown by WCF
    // services when ServiceDebugBehavior.IncludeExceptionDetailInFaults
    // is set to true.
    catch (FaultException faultEx)
    {
      Console.WriteLine("An unknown exception was received. "
        + faultEx.Message
        + faultEx.StackTrace
      );
      Console.Read();
      wcfClient.Abort();
    }
    // Standard communication fault handler.
    catch (CommunicationException commProblem)
    {
      Console.WriteLine("There was a communication problem. " + commProblem.Message + commProblem.StackTrace);
      Console.Read();
      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 configuration 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()
    Catch timeProblem As TimeoutException
      Console.WriteLine("The service operation timed out. " & timeProblem.Message)
      wcfClient.Abort()
      Console.ReadLine()
    ' Catch the contractually specified SOAP fault raised here as an exception.
    Catch greetingFault As FaultException(Of GreetingFault)
      Console.WriteLine(greetingFault.Detail.Message)
      Console.Read()
      wcfClient.Abort()
    ' Catch unrecognized faults. This handler receives exceptions thrown by WCF
    ' services when ServiceDebugBehavior.IncludeExceptionDetailInFaults 
    ' is set to true.
    Catch faultEx As FaultException
      Console.WriteLine("An unknown exception was received. " & faultEx.Message + faultEx.StackTrace)
      Console.Read()
      wcfClient.Abort()
    ' Standard communication fault handler.
    Catch commProblem As CommunicationException
      Console.WriteLine("There was a communication problem. " & commProblem.Message + commProblem.StackTrace)
      Console.Read()
      wcfClient.Abort()
    End Try
  End Sub
End Class

注解

在服务中,使用 FaultException 类创建非类型化错误以返回到客户端用于调试。

在客户端,捕获 FaultException 对象以处理未知或一般性错误,例如将 IncludeExceptionDetailInFaults 属性设置为 true 时由服务返回的错误。 因为 FaultException 扩展 CommunicationException,所以如果您要分别捕获对象,请记住先捕获所有的 FaultException 对象,再捕获 CommunicationException 对象。

备注

双工服务也能从它们与双工客户端的交互中捕获 FaultException 对象。

一般而言,强烈建议您在决定客户端需要错误信息的所有错误情况下,使用 FaultContractAttribute 设计服务以返回强类型 SOAP 错误(且非托管异常对象)。 是在以下情况中,请使用 FaultException

  • 从服务中发送 SOAP 错误以用于调试。

  • 当错误不是服务协定的一部分时,在客户端捕获 SOAP 错误。

如果希望字符串传递到构造函数后由客户端通过调用 FaultException 方法对其进行检索时,则引发 FaultException<TDetail>.ToString 对象。 如果指定了 System.ServiceModel.FaultException<TDetail> 类型的错误协定,其中类型参数为 System.String,那么字符串值可用作 FaultException<TDetail>.Detail 属性,并且不是通过调用 FaultException<TDetail>.ToString

有关详细信息,请参阅 在协定和服务中指定和处理错误

构造函数

FaultException()

初始化 FaultException 类的新实例。

FaultException(FaultReason)

使用指定的原因初始化 FaultException 类的新实例。

FaultException(FaultReason, FaultCode)

使用指定的原因和错误代码初始化 FaultException 类的新实例。

FaultException(FaultReason, FaultCode, String)

使用指定的原因、错误代码和操作值初始化 FaultException 类的新实例。

FaultException(MessageFault)

使用指定的消息错误值初始化 FaultException 类的新实例。

FaultException(MessageFault, String)

使用指定的消息错误值和提供的操作字符串初始化 FaultException 类的新实例。

FaultException(SerializationInfo, StreamingContext)

在将流反序列化到 FaultException 对象时,使用指定的序列化信息和上下文初始化 FaultException 类的新实例。

FaultException(String)

用指定的错误原因初始化 FaultException 类的新实例。

FaultException(String, FaultCode)

使用指定的原因和 SOAP 错误代码初始化 FaultException 类的新实例。

FaultException(String, FaultCode, String)

使用指定的原因、错误代码和操作值初始化 FaultException 类的新实例。

属性

Action

获取错误消息的 SOAP 操作值。

Code

获取 SOAP 错误的错误代码。

Data

获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。

(继承自 Exception)
HelpLink

获取或设置指向与此异常关联的帮助文件链接。

(继承自 Exception)
HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。

(继承自 Exception)
InnerException

获取导致当前异常的 Exception 实例。

(继承自 Exception)
Message

获取异常消息。

Reason

获取 SOAP 错误的 FaultReason

Source

获取或设置导致错误的应用程序或对象的名称。

(继承自 Exception)
StackTrace

获取调用堆栈上的即时框架字符串表示形式。

(继承自 Exception)
TargetSite

获取引发当前异常的方法。

(继承自 Exception)

方法

CreateFault(MessageFault, String, Type[])

从指定的消息错误、操作和详细信息类型的数组返回一个 FaultException 对象。

CreateFault(MessageFault, Type[])

从指定的消息错误和详细信息类型的数组返回 FaultException 对象。

CreateMessageFault()

返回一个 MessageFault 对象。

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。

(继承自 Exception)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetObjectData(SerializationInfo, StreamingContext)

实现在将对象序列化到流中时调用的 GetObjectData(SerializationInfo, StreamingContext) 方法。

GetObjectData(SerializationInfo, StreamingContext)

当在派生类中重写时,用关于异常的信息设置 SerializationInfo

(继承自 Exception)
GetType()

获取当前实例的运行时类型。

(继承自 Exception)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

创建并返回当前异常的字符串表示形式。

(继承自 Exception)

事件

SerializeObjectState
已过时。

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。

(继承自 Exception)

适用于