簡體   English   中英

使用IErrorHandler進行WCF異常處理

[英]WCF exception handling using IErrorHandler

我基本上實現IErrorHandler接口來捕獲來自WCF服務的各種異常,並通過實現ProvideFault方法將其發送到客戶端。

然而,我面臨一個關鍵問題。 所有異常都作為FaultException發送到客戶端,但這會禁止客戶端處理他可能在服務中定義的特定異常。

考慮: SomeException已在其中一個OperationContract實現中定義和拋出。 拋出異常時,使用以下代碼將其轉換為錯誤:

var faultException = new FaultException(error.Message);
MessageFault messageFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault, faultException.Action);

這會將錯誤作為字符串發送,但客戶端必須捕獲一般異常,例如:

try{...}
catch(Exception e){...}

並不是:

try{...}
catch(SomeException e){...}

不僅像SomeException這樣的自定義異常,而且使用上述過程也無法捕獲像InvalidOperationException這樣的系統異常。

關於如何實現這種行為的任何想法?

在WCF中,希望使用描述為契約的特殊異常,因為您的客戶端可能不是.NET應用程序,它具有有關標准.NET異常的信息。 為此,您可以在服務中定義FaultContract ,然后使用FaultException類。

服務器端

[ServiceContract]
public interface ISampleService
{
    [OperationContract]
    [FaultContractAttribute(typeof(MyFaultMessage))]
    string SampleMethod(string msg);
}

[DataContract]
public class MyFaultMessage
{
    public MyFaultMessage(string message)
    {
        Message = message;
    }

    [DataMember]
    public string Message { get; set; }
}

class SampleService : ISampleService
{
    public string SampleMethod(string msg)
    {
        throw new FaultException<MyFaultMessage>(new MyFaultMessage("An error occurred."));
    }        
}

此外,您可以在配置文件中指定服務器在其FaultExceptions中返回異常詳細信息,但不建議在生產應用程序中使用此方法:

<serviceBehaviors>
   <behavior>
   <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
   </behavior>
</serviceBehaviors>

之后,您可以重寫處理異常的方法:

var faultException = error as FaultException;
if (faultException == null)
{
    //If includeExceptionDetailInFaults = true, the fault exception with details will created by WCF.
    return;
}
MessageFault messageFault = faultException.CreateMessageFault();
fault = Message.CreateMessage(version, messageFault, faultException.Action);

客戶:

try
{
    _client.SampleMethod();
}
catch (FaultException<MyFaultMessage> e)
{
    //Handle            
}
catch (FaultException<ExceptionDetail> exception)
{
    //Getting original exception detail if includeExceptionDetailInFaults = true
    ExceptionDetail exceptionDetail = exception.Detail;
}

本文可能有所幫助:

http://www.olegsych.com/2008/07/simplifying-wcf-using-exceptions-as-faults/

當我不想枚舉可能拋出的異常時,我已經成功使用的一種方法是創建一個PassthroughExceptionHandlingBehavior類,它實現服務器端的IErrorHandler行為和客戶端的IClientMessageInspector。 IErrorHandler行為將異常序列化為故障消息。 IClientMessageInspector反序列化並拋出異常。

您必須將此行為附加到WCF客戶端和WCF服務器。 您可以使用配置文件或通過將[PassthroughExceptionHandlingBehavior]屬性應用於合同來附加行為。

這是行為類:

public class PassthroughExceptionHandlingBehavior : Attribute, IClientMessageInspector, IErrorHandler,
    IEndpointBehavior, IServiceBehavior, IContractBehavior
{
    #region IClientMessageInspector Members

    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
        if (reply.IsFault)
        {
            // Create a copy of the original reply to allow default processing of the message
            MessageBuffer buffer = reply.CreateBufferedCopy(Int32.MaxValue);
            Message copy = buffer.CreateMessage();  // Create a copy to work with
            reply = buffer.CreateMessage();         // Restore the original message

            var exception = ReadExceptionFromFaultDetail(copy) as Exception;
            if (exception != null)
            {
                throw exception;
            }
        }
    }

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
    {
        return null;
    }

    private static object ReadExceptionFromFaultDetail(Message reply)
    {
        const string detailElementName = "detail";

        using (XmlDictionaryReader reader = reply.GetReaderAtBodyContents())
        {
            // Find <soap:Detail>
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element && 
                    detailElementName.Equals(reader.LocalName, StringComparison.InvariantCultureIgnoreCase))
                {
                    return ReadExceptionFromDetailNode(reader);
                }
            }
            // Couldn't find it!
            return null;
        }
    }

    private static object ReadExceptionFromDetailNode(XmlDictionaryReader reader)
    {
        // Move to the contents of <soap:Detail>
        if (!reader.Read())
        {
            return null;
        }

        // Return the deserialized fault
        try
        {
            NetDataContractSerializer serializer = new NetDataContractSerializer();
            return serializer.ReadObject(reader);
        }
        catch (SerializationException)
        {
            return null;
        }
    }

    #endregion

    #region IErrorHandler Members

    public bool HandleError(Exception error)
    {
        return false;
    }

    public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
    {
        if (error is FaultException)
        {
            // Let WCF do normal processing
        }
        else
        {
            // Generate fault message manually including the exception as the fault detail
            MessageFault messageFault = MessageFault.CreateFault(
                new FaultCode("Sender"),
                new FaultReason(error.Message),
                error,
                new NetDataContractSerializer());
            fault = Message.CreateMessage(version, messageFault, null);
        }
    }

    #endregion

    #region IContractBehavior Members

    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
        ApplyClientBehavior(clientRuntime);
    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        ApplyDispatchBehavior(dispatchRuntime.ChannelDispatcher);
    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {
    }

    #endregion

    #region IEndpointBehavior Members

    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        ApplyClientBehavior(clientRuntime);
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
        ApplyDispatchBehavior(endpointDispatcher.ChannelDispatcher);
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }

    #endregion

    #region IServiceBehavior Members

    public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
        {
            ApplyDispatchBehavior(dispatcher);
        }
    }

    public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
    {
    }

    #endregion

    #region Behavior helpers

    private static void ApplyClientBehavior(System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        foreach (IClientMessageInspector messageInspector in clientRuntime.MessageInspectors)
        {
            if (messageInspector is PassthroughExceptionHandlingBehavior)
            {
                return;
            }
        }

        clientRuntime.MessageInspectors.Add(new PassthroughExceptionHandlingBehavior());
    }

    private static void ApplyDispatchBehavior(System.ServiceModel.Dispatcher.ChannelDispatcher dispatcher)
    {
        // Don't add an error handler if it already exists
        foreach (IErrorHandler errorHandler in dispatcher.ErrorHandlers)
        {
            if (errorHandler is PassthroughExceptionHandlingBehavior)
            {
                return;
            }
        }

        dispatcher.ErrorHandlers.Add(new PassthroughExceptionHandlingBehavior());
    }

    #endregion
}

#region PassthroughExceptionHandlingElement class

public class PassthroughExceptionExtension : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get { return typeof(PassthroughExceptionHandlingBehavior); }
    }

    protected override object CreateBehavior()
    {
        System.Diagnostics.Debugger.Launch();
        return new PassthroughExceptionHandlingBehavior();
    }
}

#endregion

FaultException有一個Code屬性,您可以使用前面的異常處理。

try
{
...
}
catch (FaultException ex)
{
   switch(ex.Code)
   {
       case 0:
          break;
       ...
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM