FaultException<TDetail> FaultException<TDetail> FaultException<TDetail> FaultException<TDetail> Class

定义

用于在客户端应用程序中捕获通过协定方式指定的 SOAP 错误。Used in a client application to catch contractually-specified SOAP faults.

generic <typename TDetail>
public ref class FaultException : System::ServiceModel::FaultException
[System.Serializable]
public class FaultException<TDetail> : System.ServiceModel.FaultException
type FaultException<'Detail> = class
    inherit FaultException
Public Class FaultException(Of TDetail)
Inherits FaultException

类型参数

TDetail

可序列化错误详细信息类型。The serializable error detail type.

继承
派生
属性

示例

下面的代码示例演示服务如何使用 FaultException<TDetail> 类型引发将转换为由 FaultContractAttribute 指定的 SOAP 错误的托管异常。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
  }
}

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

下面的代码示例演示客户端代码如何在由使用的元数据实用工具 (svcutil.exe)的客户端导入时进行查看。The following code example shows how the client code looks when imported by the client using the ServiceModel Metadata Utility Tool (Svcutil.exe).

下面的代码示例演示客户端如何捕获 FaultException<TDetail> 类型,该类型表示操作协定中指定的自定义 SOAP 错误。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();
    }
  }
}

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

注解

捕获 Windows Communication Foundation FaultException<TDetail> (WCF) 客户端应用程序中的对象, 以处理已合同形式在操作协定中指定的 SOAP 错误。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.

已典型部署的服务使用 FaultContractAttribute 来正式指定在正常操作过程中客户端可预期收到的所有 SOAP 错误。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. 当它到达客户FaultContractAttribute端应用程序FaultException<TDetail>时, 中的错误消息将显示为 (其中, typeparameter 是FaultContractAttribute操作的中指定的可序列化错误对象)。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. FaultContractAttribute 可用于指定双向服务方法和异步方法对的 SOAP 错误。The FaultContractAttribute can be used to specify SOAP faults for both two-way service methods and for asynchronous method pairs.

由于 FaultException<TDetail> 是一个 FaultException,因此也是一个 CommunicationException,若要捕获指定的 SOAP 错误请确保您在捕获 FaultException<TDetail>FaultException 类型之前捕获 CommunicationException 类型,或者使用其中的一个异常处理程序来处理指定的异常。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.

备注

如果您使用 System.ServiceModel.FaultContractAttribute 指定 FaultException<TDetail>,其中类型参数为 System.String,则字符串值将分配给客户端应用程序中的 Detail 属性;客户端无法通过调用 FaultException<TDetail>.ToString 方法检索该字符串。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. 若要在客户端应用程序调用 Exception.ToString 时返回该字符串值,则将在操作内引发 System.ServiceModel.FaultException 异常,并将该字符串传递给构造函数。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.
通常情况下,建议将相应的自定义可序列化类型作为错误的详细信息类型,而不是使用 System.StringIn general, it is recommended that detail types be custom serializable types appropriate to the fault and not a System.String.

构造函数

FaultException<TDetail>(SerializationInfo, StreamingContext) FaultException<TDetail>(SerializationInfo, StreamingContext) FaultException<TDetail>(SerializationInfo, StreamingContext) FaultException<TDetail>(SerializationInfo, StreamingContext)

在将流反序列化到 FaultException<TDetail> 对象时,使用指定的序列化信息和上下文初始化 FaultException 类的新实例。Initializes a new instance of the FaultException<TDetail> class using the specified serialization information and context when deserializing a stream into a FaultException object.

FaultException<TDetail>(TDetail) FaultException<TDetail>(TDetail) FaultException<TDetail>(TDetail) FaultException<TDetail>(TDetail)

初始化使用指定详细信息对象的 FaultException<TDetail> 类的新实例。Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object.

FaultException<TDetail>(TDetail, FaultReason) FaultException<TDetail>(TDetail, FaultReason) FaultException<TDetail>(TDetail, FaultReason) FaultException<TDetail>(TDetail, FaultReason)

初始化使用指定详细信息对象和错误原因的 FaultException<TDetail> 类的新实例。Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object and fault reason.

FaultException<TDetail>(TDetail, FaultReason, FaultCode) FaultException<TDetail>(TDetail, FaultReason, FaultCode) FaultException<TDetail>(TDetail, FaultReason, FaultCode) FaultException<TDetail>(TDetail, FaultReason, FaultCode)

初始化 FaultException<TDetail> 类的新实例,该类使用指定的详细信息对象、错误原因和错误代码。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) FaultException<TDetail>(TDetail, FaultReason, FaultCode, String) FaultException<TDetail>(TDetail, FaultReason, FaultCode, String) FaultException<TDetail>(TDetail, FaultReason, FaultCode, String)

初始化 FaultException<TDetail> 类的新实例,该类使用指定的详细信息对象以及 SOAP 错误原因、代码和操作值。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) FaultException<TDetail>(TDetail, String) FaultException<TDetail>(TDetail, String) FaultException<TDetail>(TDetail, String)

初始化使用指定详细信息和错误原因的 FaultException<TDetail> 类的新实例。Initializes a new instance of the FaultException<TDetail> class that uses the specified detail and fault reason.

FaultException<TDetail>(TDetail, String, FaultCode) FaultException<TDetail>(TDetail, String, FaultCode) FaultException<TDetail>(TDetail, String, FaultCode) FaultException<TDetail>(TDetail, String, FaultCode)

初始化 FaultException<TDetail> 类的新实例,该类使用指定的详细信息对象、错误原因和错误代码。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) FaultException<TDetail>(TDetail, String, FaultCode, String) FaultException<TDetail>(TDetail, String, FaultCode, String) FaultException<TDetail>(TDetail, String, FaultCode, String)

初始化 FaultException<TDetail> 类的新实例,该类使用指定的详细信息对象以及 SOAP 错误原因、代码和操作值。Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object, and SOAP fault reason, code and action values.

属性

Action Action Action Action

获取错误消息的 SOAP 操作值。Gets the value of the SOAP action for the fault message.

(Inherited from FaultException)
Code Code Code Code

获取 SOAP 错误的错误代码。Gets the fault code for the SOAP fault.

(Inherited from FaultException)
Data Data Data Data

获取提供有关异常的其他用户定义信息的键/值对集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
Detail Detail Detail Detail

获取包含错误条件详细信息的对象。Gets the object that contains the detail information of the fault condition.

HelpLink HelpLink HelpLink HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult HResult HResult HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException InnerException InnerException InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message Message Message Message

获取异常消息。Gets the message for the exception.

(Inherited from FaultException)
Reason Reason Reason Reason

获取 SOAP 错误的 FaultReasonGets the FaultReason for the SOAP fault.

(Inherited from FaultException)
Source Source Source Source

获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)
StackTrace StackTrace StackTrace StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(Inherited from Exception)

方法

CreateMessageFault() CreateMessageFault() CreateMessageFault() CreateMessageFault()

创建一个 MessageFault 对象,该对象可用于创建表示 SOAP 错误的 MessageCreates a MessageFault object that can be used to create a Message that represents the SOAP fault.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

实现在将对象序列化到流中时调用的 GetObjectData(SerializationInfo, StreamingContext) 方法。Implementation of the GetObjectData(SerializationInfo, StreamingContext) method that is called when the object is serialized into a stream.

GetType() GetType() GetType() GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回 FaultException<TDetail> 对象的字符串。Returns a string for the FaultException<TDetail> object.

事件

SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

适用于