FaultException<TDetail> 類別

定義

在用戶端應用程式中用於攔截以合約方式指定的 SOAP 錯誤。

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

類型參數

TDetail

可序列化的錯誤詳細類型。

繼承
繼承
衍生
屬性

範例

下列程式碼範例示範服務如何使用 FaultException<TDetail> 型別,擲回轉換成由 FaultContractAttribute 所指定之 SOAP 錯誤的 Managed 例外狀況。

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

下列程式代碼範例顯示用戶端使用 ServiceModel元數據公用程式工具匯 入時,用戶端程式代碼的外觀 (Svcutil.exe) 。

下列程式碼範例示範用戶端如何攔截 FaultException<TDetail> 型別,該型別表示在作業合約中指定的自訂 SOAP 錯誤。

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

備註

FaultException<TDetail>攔截 Windows Communication Foundation (WCF) 用戶端應用程式中的 物件,以處理已在作業合約中指定合約的 SOAP 錯誤。

一般部署的服務會使用 FaultContractAttribute 來正式指定所有的 SOAP 錯誤,用戶端可預期以一般正常的作業過程來接收。 中的 FaultContractAttribute 錯誤資訊會顯示為 FaultException<TDetail> (,其中 typeparameter 是作業 FaultContractAttribute) 到達客戶端應用程式時所指定的可串行化錯誤物件。 FaultContractAttribute 可用於指定雙向服務方法和非同步方法組的 SOAP 錯誤。

由於 FaultException<TDetail>FaultException 因而也是 CommunicationException,如果要攔截指定的 SOAP 錯誤,請確定在 FaultException<TDetail>FaultException 型別之前,先攔截 CommunicationException 型別,或在其中一個例外狀況處理常式中處理指定的例外狀況。

注意

如果您使用 System.ServiceModel.FaultContractAttribute 來指定 FaultException<TDetail>,其中型別參數為 System.String,字串值會指派給用戶端應用程式中的 Detail 屬性;用戶端無法藉由呼叫 FaultException<TDetail>.ToString 方法來擷取該字串。 若要在用戶端應用程式呼叫 Exception.ToString 時傳回字串值,請在該作業內擲回一個 System.ServiceModel.FaultException 例外狀況,並將字串傳遞給建構函式。 一般而言,建議詳細類型為適合錯誤的自訂可序列化型別,而不是 System.String

建構函式

FaultException<TDetail>(SerializationInfo, StreamingContext)

將資料流還原序列化至 FaultException<TDetail> 物件時,使用指定的序列化資訊和內容,初始化 FaultException 類別的新執行個體。

FaultException<TDetail>(TDetail)

初始化 FaultException<TDetail> 類別的新執行個體,這個執行個體使用指定的詳細資料物件。

FaultException<TDetail>(TDetail, FaultReason)

初始化 FaultException<TDetail> 類別的新執行個體,這個執行個體使用指定的詳細資料物件和錯誤原因。

FaultException<TDetail>(TDetail, FaultReason, FaultCode)

初始化 FaultException<TDetail> 類別的新執行個體,這個執行個體使用指定的詳細資料物件、錯誤原因和錯誤碼。

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

初始化 FaultException<TDetail> 類別的新執行個體,這個執行個體使用指定的詳細資料物件、SOAP 錯誤原因、錯誤碼和動作值。

FaultException<TDetail>(TDetail, String)

初始化 FaultException<TDetail> 類別的新執行個體,這個執行個體使用指定的詳細資料和錯誤原因。

FaultException<TDetail>(TDetail, String, FaultCode)

初始化 FaultException<TDetail> 類別的新執行個體,這個執行個體使用指定的詳細資料物件、錯誤原因和錯誤碼。

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

初始化 FaultException<TDetail> 類別的新執行個體,這個執行個體使用指定的詳細資料物件、SOAP 錯誤原因、錯誤碼和動作值。

屬性

Action

取得錯誤訊息的 SOAP 動作值。

(繼承來源 FaultException)
Code

取得 SOAP 錯誤的錯誤碼。

(繼承來源 FaultException)
Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
Detail

取得物件,其中包含錯誤狀況的詳細資訊。

HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
Message

取得例外狀況的訊息。

(繼承來源 FaultException)
Reason

取得 SOAP 錯誤的 FaultReason

(繼承來源 FaultException)
Source

取得或設定造成錯誤的應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)

方法

CreateMessageFault()

建立可用於建立表示 SOAP 錯誤之 MessageFaultMessage 物件。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

GetObjectData(SerializationInfo, StreamingContext) 方法的實作,當物件序列化成資料流時,會呼叫此方法。

GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

GetObjectData(SerializationInfo, StreamingContext) 方法的實作,當物件序列化成資料流時,會呼叫此方法。

(繼承來源 FaultException)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回 FaultException<TDetail> 物件的字串。

事件

SerializeObjectState
已淘汰.

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於