共用方式為


IErrorHandler 介面

定義

可讓實作器控制傳回到呼叫端的錯誤訊息,並選擇性地執行自訂錯誤處理,例如記錄。

public interface class IErrorHandler
public interface IErrorHandler
type IErrorHandler = interface
Public Interface IErrorHandler

範例

下列程式碼範例將示範實作 IErrorHandler 的服務,當服務方法擲回 Managed 例外狀況時,這個實作只會傳回 FaultException<TDetail> 類型的 GreetingFault

#region IErrorHandler Members
public bool HandleError(Exception error)
{
  Console.WriteLine("HandleError called.");
  // Returning true indicates you performed your behavior.
  return true;
}

// This is a trivial implementation that converts Exception to FaultException<GreetingFault>.
public void ProvideFault(
  Exception error,
  MessageVersion ver,
  ref Message msg
)
{
  Console.WriteLine("ProvideFault called. Converting Exception to GreetingFault....");
  FaultException<GreetingFault> fe
    = new FaultException<GreetingFault>(new GreetingFault(error.Message));
  MessageFault fault = fe.CreateMessageFault();
  msg = Message.CreateMessage(
    ver,
    fault,
    "http://microsoft.wcf.documentation/ISampleService/SampleMethodGreetingFaultFault"
  );
}
#endregion
#Region "IErrorHandler Members"
Public Function HandleError(ByVal [error] As Exception) As Boolean Implements IErrorHandler.HandleError
  Console.WriteLine("HandleError called.")
  ' Returning true indicates you performed your behavior.
  Return True
End Function

' This is a trivial implementation that converts Exception to FaultException<GreetingFault>.
Public Sub ProvideFault(ByVal [error] As Exception, ByVal ver As MessageVersion, ByRef msg As Message) Implements IErrorHandler.ProvideFault
  Console.WriteLine("ProvideFault called. Converting Exception to GreetingFault....")
  Dim fe As New FaultException(Of GreetingFault)(New GreetingFault([error].Message))
  Dim fault As MessageFault = fe.CreateMessageFault()
  msg = Message.CreateMessage(ver, fault, "http://microsoft.wcf.documentation/ISampleService/SampleMethodGreetingFaultFault")
End Sub
#End Region

下列程式碼範例示範如何使用服務行為,將 IErrorHandler 實作加入至 ErrorHandlers 屬性。

// This behavior modifies no binding parameters.
#region IServiceBehavior Members
public void AddBindingParameters(
  ServiceDescription description,
  ServiceHostBase serviceHostBase,
  System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
  System.ServiceModel.Channels.BindingParameterCollection parameters
)
{
  return;
}

// This behavior is an IErrorHandler implementation and
// must be applied to each ChannelDispatcher.
public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
{
  Console.WriteLine("The EnforceGreetingFaultBehavior has been applied.");
  foreach(ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)
  {
    chanDisp.ErrorHandlers.Add(this);
  }
}

// This behavior requires that the contract have a SOAP fault with a detail type of GreetingFault.
public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
{
  Console.WriteLine("Validate is called.");
  foreach (ServiceEndpoint se in description.Endpoints)
  {
    // Must not examine any metadata endpoint.
    if (se.Contract.Name.Equals("IMetadataExchange")
      && se.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
      continue;
    foreach (OperationDescription opDesc in se.Contract.Operations)
    {
      if (opDesc.Faults.Count == 0)
        throw new InvalidOperationException(String.Format(
          "EnforceGreetingFaultBehavior requires a "
          + "FaultContractAttribute(typeof(GreetingFault)) in each operation contract.  "
          + "The \"{0}\" operation contains no FaultContractAttribute.",
          opDesc.Name)
        );
      bool gfExists = false;
      foreach (FaultDescription fault in opDesc.Faults)
      {
        if (fault.DetailType.Equals(typeof(GreetingFault)))
        {
          gfExists = true;
          continue;
        }
      }
      if (gfExists == false)
      {
        throw new InvalidOperationException(
"EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract."
        );
      }
    }
  }
}
#endregion
' This behavior modifies no binding parameters.
#Region "IServiceBehavior Members"
Public Sub AddBindingParameters(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase, ByVal endpoints As System.Collections.ObjectModel.Collection(Of ServiceEndpoint), ByVal parameters As System.ServiceModel.Channels.BindingParameterCollection) Implements IServiceBehavior.AddBindingParameters
  Return
End Sub

' This behavior is an IErrorHandler implementation and 
' must be applied to each ChannelDispatcher.
Public Sub ApplyDispatchBehavior(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase) Implements IServiceBehavior.ApplyDispatchBehavior
  Console.WriteLine("The EnforceGreetingFaultBehavior has been applied.")
  For Each chanDisp As ChannelDispatcher In serviceHostBase.ChannelDispatchers
    chanDisp.ErrorHandlers.Add(Me)
  Next chanDisp
End Sub

' This behavior requires that the contract have a SOAP fault with a detail type of GreetingFault.
Public Sub Validate(ByVal description As ServiceDescription, ByVal serviceHostBase As ServiceHostBase) Implements IServiceBehavior.Validate
  Console.WriteLine("Validate is called.")
  For Each se As ServiceEndpoint In description.Endpoints
    ' Must not examine any metadata endpoint.
    If se.Contract.Name.Equals("IMetadataExchange") AndAlso se.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex") Then
      Continue For
    End If
    For Each opDesc As OperationDescription In se.Contract.Operations
      If opDesc.Faults.Count = 0 Then
        Throw New InvalidOperationException(String.Format("EnforceGreetingFaultBehavior requires a " & "FaultContractAttribute(typeof(GreetingFault)) in each operation contract.  " & "The ""{0}"" operation contains no FaultContractAttribute.", opDesc.Name))
      End If
      Dim gfExists As Boolean = False
      For Each fault As FaultDescription In opDesc.Faults
        If fault.DetailType.Equals(GetType(GreetingFault)) Then
          gfExists = True
          Continue For
        End If
      Next fault
      If gfExists = False Then
        Throw New InvalidOperationException("EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract.")
      End If
    Next opDesc
  Next se
End Sub
#End Region

下列程式碼範例示範如何使用應用程式組態檔,將服務設定為載入服務行為。 如需如何在組態檔中公開服務行為的詳細資訊,請參閱 IServiceBehavior

<configuration>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="metaAndErrors">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/SampleService"/>
          </baseAddresses>
        </host>
        <endpoint
          address=""
          binding="wsHttpBinding"
          contract="Microsoft.WCF.Documentation.ISampleService"
         />
        <endpoint
          address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange"
         />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metaAndErrors">
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceMetadata httpGetEnabled="true"/>
          <enforceGreetingFaults />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add 
          name="enforceGreetingFaults" 
          type="Microsoft.WCF.Documentation.EnforceGreetingFaultBehavior, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
        />
      </behaviorExtensions>
    </extensions>
  </system.serviceModel>
</configuration>

備註

若要在擲回例外狀況時明確控制應用程式的行為,請實 IErrorHandler 作 介面,並將其新增至 ChannelDispatcherErrorHandlers 屬性。 IErrorHandler 可讓您明確地控制產生的 SOAP 錯誤,決定是否要將它傳送回用戶端,以及執行相關的工作 (例如,記錄)。 錯誤處理常式會依其加入至 ErrorHandlers 屬性的順序進行呼叫。

實作 ProvideFault 方法,即可控制傳回到用戶端的錯誤訊息。

實作 HandleError 方法,即可確認錯誤相關行為,其中包括記錄錯誤、確保 Fail Fast (失敗時,即刻傳回錯誤訊息,然後才停止)、關閉應用程式等。

注意

因為 HandleError 方法可從許多不同地方呼叫,所以不保證此方法會在哪一個執行緒上呼叫。 請不要依賴作業執行緒上呼叫的 HandleError 方法。

在傳送回應訊息之前,會先呼叫所有的 ProvideFault 實作。 在呼叫並回傳所有的 ProvideFault 實作之後,以及在 fault 不是 null 時,就會根據作業合約將該訊息傳送回用戶端。 在呼叫所有實作之後,如果 faultnull,則會由 ServiceBehaviorAttribute.IncludeExceptionDetailInFaults 屬性值來控制回應訊息。

注意

例外狀況可能會在呼叫所有 ProvideFault 實作,而且將回應訊息傳送至通道之後發生。 例如,如果通道例外狀況發生 (,則會收到通知) 物件的難以序列化訊息 IErrorHandler 。 在此情況下,您仍應該確定您的開發環境攔截並顯示這類例外狀況給您,或使用追蹤來探索問題。 如需追蹤的詳細資訊,請參閱 使用追蹤對您的應用程式進行疑難排解

在傳送回應訊息之後,會依照相同順序呼叫所有的 HandleError 實作。

一般而言,IErrorHandler 實作會加入至服務 (若是雙工通訊,則為用戶端) 上的 ErrorHandlers 屬性。

您可以藉由實作某種行為 (IErrorHandlerSystem.ServiceModel.Description.IServiceBehaviorSystem.ServiceModel.Description.IEndpointBehaviorSystem.ServiceModel.Description.IContractBehavior 物件),將 System.ServiceModel.Description.IOperationBehavior 加入至執行階段,並且從組態檔中或透過自訂屬性,以程式設計方式使用該行為來附加您的 IErrorHandler

如需使用行為來修改執行時間的詳細資訊,請參閱 使用行為設定和擴充運行時間。

方法

HandleError(Exception)

啟用錯誤相關處理並傳回值,指出發送器是否會在某些情況下,中止工作階段和執行個體內容。

ProvideFault(Exception, MessageVersion, Message)

啟用建立由服務方法發生之例外狀況所傳回的自訂 FaultException<TDetail>

適用於