FaultException<TDetail> 類別

定義

在用戶端應用程式中用於攔截以合約方式指定的 SOAP 錯誤。Used in a client application to catch contractually-specified SOAP faults.

generic <typename TDetail>
public ref class FaultException : System::ServiceModel::FaultException
public class FaultException<TDetail> : 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 錯誤的 Managed 例外狀況。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

下列程式碼範例會示範用戶端如何使用System.servicemodel 中繼資料公用程式工具(Svcutil)來匯入用戶端程式代碼。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 (WCF)用戶端應用程式中的 FaultException<TDetail> 物件,以處理已依履約在作業合約中指定的 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中指定的 serializable 錯誤物件)。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> 物件時,使用指定的序列化資訊和內容,初始化 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> 類別的新執行個體,這個執行個體使用指定的詳細資料物件。Initializes a new instance of the FaultException<TDetail> class that uses the specified detail object.

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> 類別的新執行個體,這個執行個體使用指定的詳細資料物件、錯誤原因和錯誤碼。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> 類別的新執行個體,這個執行個體使用指定的詳細資料物件、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> 類別的新執行個體,這個執行個體使用指定的詳細資料和錯誤原因。Initializes a new instance of the FaultException<TDetail> class that uses the specified detail and fault reason.

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> 類別的新執行個體,這個執行個體使用指定的詳細資料物件、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

取得錯誤訊息的 SOAP 動作值。Gets the value of the SOAP action for the fault message.

(繼承來源 FaultException)
Code

取得 SOAP 錯誤的錯誤碼。Gets the fault code for the SOAP fault.

(繼承來源 FaultException)
Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(繼承來源 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.

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(繼承來源 Exception)
Message

取得例外狀況的訊息。Gets the message for the exception.

(繼承來源 FaultException)
Reason

取得 SOAP 錯誤的 FaultReasonGets the FaultReason for the SOAP fault.

(繼承來源 FaultException)
Source

取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(繼承來源 Exception)

方法

CreateMessageFault()

建立可用於建立表示 SOAP 錯誤之 MessageFaultMessage 物件。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.

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

GetObjectData(SerializationInfo, StreamingContext) 方法的實作,當物件序列化成資料流時,會呼叫此方法。Implementation of the GetObjectData(SerializationInfo, StreamingContext) method that is called when the object is serialized into a stream.

GetObjectData(SerializationInfo, StreamingContext)

GetObjectData(SerializationInfo, StreamingContext) 方法的實作,當物件序列化成資料流時,會呼叫此方法。Implementation of the GetObjectData(SerializationInfo, StreamingContext) method that is called when the object is serialized into a stream.

(繼承來源 FaultException)
GetType()

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

傳回 FaultException<TDetail> 物件的字串。Returns a string for the FaultException<TDetail> object.

事件

SerializeObjectState

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(繼承來源 Exception)

適用於