CommunicationException 類別

定義

代表服務或用戶端應用程式的通訊錯誤。Represents a communication error in either the service or client application.

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

範例

下列程式碼範例示範處理 CommunicationException 型別的用戶端。The following code example shows a client that handles CommunicationException types. 這個用戶端也會處理 FaultException 物件,因為服務將 IncludeExceptionDetailInFaults 設為 trueThis client also handles FaultException objects because the service has IncludeExceptionDetailInFaults set to 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)應用程式會處理在通訊期間可能會擲回的 @no__t 0 物件。Robust client and service Windows Communication Foundation (WCF) applications handle CommunicationException objects that may be thrown during communication. 還有兩種衍生自 CommunicationException 的例外狀況型別 (FaultException<TDetail>FaultException),用戶端也經常會碰到。There are also two CommunicationException-derived exception types (FaultException<TDetail> and FaultException) that clients also often expect. 因此,為避免一般性 CommunicationException 處理常式攔截更明確的例外狀況型別,請在處理 CommunicationException 之前先攔截這些例外狀況。Therefore, in order to prevent the generic CommunicationException handler from catching these more specific exception types, catch these exceptions prior to handling CommunicationException.

當接聽程式收到非預期或沒有指定在作業合約中的 SOAP 錯誤時,會擲回 FaultException 物件。FaultException objects are thrown when a listener receives a SOAP fault that is not expected or specified in the operation contract. 當應用程式在進行偵錯,且服務將 IncludeExceptionDetailInFaults 屬性設為 true 時,常會發生這種情況。This usually occurs when the application is being debugged and the service has the IncludeExceptionDetailInFaults property set to true.

注意

實作自訂通道與繫結項目時,強烈建議您的元件只能擲回衍生自 System.TimeoutException or CommunicationException 的物件。When implementing custom channels and binding elements, it is strongly recommended that your components throw only System.TimeoutException or CommunicationException-derived objects. 當您的元件擲回一個專屬於該元件、且可復原的例外狀況時,請將該例外狀況包進 CommunicationException 物件。In the case where your components throw a recoverable exception that is specific to the component, wrap that exception inside a CommunicationException object.

如需設計和使用 WCF 錯誤系統的詳細資訊,請參閱指定和處理合約和服務中的錯誤For more details about designing and using the WCF fault system, see Specifying and Handling Faults in Contracts and Services.

重要

WCF 執行時間不會擲回 CommunicationException,不安全地處理它離開 WCF 執行時間並進入使用者程式碼的位置。The WCF Runtime will not throw a CommunicationException that is unsafe to handle at the point where it leaves the WCF Runtime and enters user code.

建構函式

CommunicationException()

初始化 CommunicationException 類別的新執行個體。Initializes a new instance of the CommunicationException class.

CommunicationException(SerializationInfo, StreamingContext)

使用指定的序列化資訊和內容物件,初始化 CommunicationException 類別的新執行個體。Initializes a new instance of the CommunicationException class, using the specified serialization information and context objects.

CommunicationException(String)

使用指定的訊息,初始化 CommunicationException 類別的新執行個體。Initializes a new instance of the CommunicationException class, using the specified message.

CommunicationException(String, Exception)

使用指定的訊息和內部例外狀況,初始化 CommunicationException 類別的新執行個體。Initializes a new instance of the CommunicationException class, using the specified message and the inner exception.

屬性

Data

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

(繼承來源 Exception)
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 a message that describes the current exception.

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

方法

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)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(繼承來源 Exception)
GetType()

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

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

建立並傳回目前例外狀況的字串表示。Creates and returns a string representation of the current exception.

(繼承來源 Exception)

事件

SerializeObjectState

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

(繼承來源 Exception)

適用於