CommunicationException 類別

定義

代表服務或用戶端應用程式的通訊錯誤。

public ref class CommunicationException : Exception
public ref class CommunicationException : SystemException
public class CommunicationException : Exception
[System.Serializable]
public class CommunicationException : SystemException
type CommunicationException = class
    inherit Exception
[<System.Serializable>]
type CommunicationException = class
    inherit SystemException
Public Class CommunicationException
Inherits Exception
Public Class CommunicationException
Inherits SystemException
繼承
CommunicationException
繼承
CommunicationException
衍生
屬性

範例

下列程式碼範例示範處理 CommunicationException 型別的用戶端。 這個用戶端也會處理 FaultException 物件,因為服務將 IncludeExceptionDetailInFaults 設為 true

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

備註

強固的用戶端和服務Windows Communication Foundation (WCF) 應用程式會處理 CommunicationException 在通訊期間可能會擲回的物件。 還有兩種衍生自 CommunicationException 的例外狀況型別 (FaultException<TDetail>FaultException),用戶端也經常會碰到。 因此,為避免一般性 CommunicationException 處理常式攔截更明確的例外狀況型別,請在處理 CommunicationException 之前先攔截這些例外狀況。

FaultException 當接聽程式收到作業合約中未預期或指定的 SOAP 錯誤時,會擲回 物件。 當應用程式在進行偵錯,且服務將 IncludeExceptionDetailInFaults 屬性設為 true 時,常會發生這種情況。

注意

實作自訂通道與繫結項目時,強烈建議您的元件只能擲回衍生自 System.TimeoutException or CommunicationException 的物件。 當您的元件擲回一個專屬於該元件、且可復原的例外狀況時,請將該例外狀況包進 CommunicationException 物件。

如需設計和使用 WCF 錯誤系統的詳細資訊,請參閱 在合約和服務中指定及處理錯誤

重要

WCF 執行時間不會在 CommunicationException 離開 WCF 執行時間並輸入使用者程式碼的點擲回不安全的 。

建構函式

CommunicationException()

初始化 CommunicationException 類別的新執行個體。

CommunicationException(SerializationInfo, StreamingContext)

使用指定的序列化資訊和內容物件,初始化 CommunicationException 類別的新執行個體。

CommunicationException(String)

使用指定的訊息,初始化 CommunicationException 類別的新執行個體。

CommunicationException(String, Exception)

使用指定的訊息和內部例外狀況,初始化 CommunicationException 類別的新執行個體。

屬性

Data

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

(繼承來源 Exception)
HelpLink

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

(繼承來源 Exception)
HResult

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

(繼承來源 Exception)
InnerException

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

(繼承來源 Exception)
Message

取得描述目前例外狀況的訊息。

(繼承來源 Exception)
Source

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

(繼承來源 Exception)
StackTrace

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

(繼承來源 Exception)
TargetSite

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

(繼承來源 Exception)

方法

Equals(Object)

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

(繼承來源 Object)
GetBaseException()

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

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

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

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfo

(繼承來源 Exception)
GetType()

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

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已過時。

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

(繼承來源 Exception)

適用於