簡體   English   中英

WCF:在IErrorHandler中提供通用的FaultException

[英]WCF: provide generic FaultException in IErrorHandler

一些上下文:我們有一個自定義XSD,並使用WSCF.blue生成WSDL和C#代碼。 客戶端使用ChannelFactory<T>並共享包含WSCF.blue添加的所有屬性的接口,以匹配XSD中的內容。

我正在嘗試實現IErrorHandler.ProvideFault ,它提供了一個通用的FaultException<T> ,但在客戶端我得到了一個非通用的FaultContract 這就是我的ProvideFault方法的樣子:

public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
    if (!(error is FaultException))
    {
        FaultException faultException = FaultExceptionFactory.CreateFaultException(error);
        MessageFault messageFault = faultException.CreateMessageFault();
        fault = Message.CreateMessage(version, messageFault, faultException.Action);
    }
}

在每個服務方法中,如果我使用throw FaultExceptionFactory.CreateFaultException(ex)執行try / catch,它按預期工作,所以我認為[FaultContract] ,factory,bindings等都是正確的。 以防萬一,這就是工廠的工作原理:

BusinessRuleFaultExceptionType businessRuleFaultException = new BusinessRuleFaultExceptionType();
BusinessRuleFaultException.Code = exception.Code.ToString();
BusinessRuleFaultException.Reason = exception.Message;
return new FaultException<BusinessRuleFaultExceptionType>(
    businessRuleFaultException,
    exception.Message,
    new FaultCode(exception.Code.ToString())
);

我認為問題在於如何在IErrorHandler創建消息,可能在CreateMessageFault() 我已經讀過,操作應該是faultException.Action而不是null ,但實際上faultException.Actionnull 也許這導致了這個問題。 我可以在工廠設置一個動作,但是動作應該是什么,為什么不會出現手動拋出?

我還有什么想法可能會遺漏?

編輯:我檢查了WSDL並找到了我正在調用的特定操作及其操作:

<wsdl:operation name="MyMethod">
<wsdl:fault wsaw:Action="http://myNamespace/MyMethodBusinessRuleFaultExceptionTypeFault" name="BusinessRuleFaultExceptionTypeFault" message="tns:..."/>

我試着硬編碼的操作: Message.CreateMessage(..., "http://myNamespace/MyMethodBusinessRuleFaultExceptionTypeFault")並將其設置為.Action在工廠,但仍然沒有工作。

編輯2: throw / catch生成以下XML,允許在客戶端上捕獲泛型異常:

<s:Fault>
    <faultcode xmlns="">s:-100</faultcode>
    <faultstring xml:lang="en-US" xmlns="">xxx</faultstring>
    <detail xmlns="">
        <BusinessRuleFaultExceptionType xmlns="http://myNamespace/Services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <Code xmlns="http://myNamespace/Entitites">-100</Code>
            <Reason xmlns="http://myNamespace/Entitites">xxx</Reason>
        </BusinessRuleFaultExceptionType>
    </detail>
</s:Fault>

IHttpErrorHandler生成以下內容,轉到非泛型FaultException

<s:Fault>
    <faultcode xmlns="">s:-100</faultcode>
    <faultstring xml:lang="en-US" xmlns="">xxx</faultstring>
    <detail xmlns="">
        <BusinessRuleFaultExceptionType xmlns="http://schemas.datacontract.org/2004/07/My.Dot.Net.Namespace" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <codeField>-100</codeField>
            <reasonField>xxx</reasonField>
        </BusinessRuleFaultExceptionType>
    </detail>
</s:Fault>

編輯3:如果我將[DataContract][DataMember]BusinessRuleFaultExceptionType ,那么我得到幾乎正確的XML:

<s:Fault>
    <faultcode xmlns="">s:-100</faultcode>
    <faultstring xml:lang="en-US" xmlns="">xxx</faultstring>
    <detail xmlns="">
        <BusinessRuleFaultExceptionType xmlns="http://myNamespace/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <Code>-100</Code>
            <Reason>xxx</Reason>
        </BusinessRuleFaultExceptionType>
    </detail>
</s:Fault>

它缺少Code和Reason的命名空間。 至少我認為這會縮小它。 常規序列化使用[XmlType][XmlElement]IErrorHandler使用[DataContract][DataMember] 不幸的是[DataMember]不允許你設置命名空間,所以我認為現在的問題是如何在IErrorHandler使用XMLSerializer。 這描述了我的問題,但由於上述原因,修復程序無效: http//twenty6-jc.blogspot.com/2011/05/ierrorhandlerprovidefault-serialization.html

編輯4:我部分發現了這個問題。 我們正在使用XmlSerializer ,但由於IErrorHandler不在操作范圍內,因此它將恢復為默認的DataContractSerializer 解決方案是更改您的服務以在任何地方使用DataContractSerializer ,或在創建故障時手動選擇XmlSerializer 這兩篇文章提供了我需要的東西:

http://twenty6-jc.blogspot.com/2011/05/ierrorhandlerprovidefault-serialization.html http://zamd.net/2008/08/15/serializing-faults-using-xmlserializer/

這讓我非常接近。 它與工作XML相同,只是缺少異常類型的命名空間:

<s:Fault>
    <faultcode xmlns="">s:-100</faultcode>
    <faultstring xml:lang="en-US" xmlns="">xxx</faultstring>
    <detail xmlns="">
        <BusinessRuleFaultExceptionType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <Code xmlns="http://myNamespace/Entitites">-100</Code>
            <Reason xmlns="http://myNamespace/Entitites">xxx</Reason>
        </BusinessRuleFaultExceptionType>
    </detail>
</s:Fault>

我假設它沒有添加xmlns="http://myNamespace/Services"因為它沒有請求的上下文。 該命名空間在界面中定義,但不是數據協定。 我是否真的會被迫留在請求的上下文中(希望IOperationInvoker有效),而不是使用IHttpHandler

我對WSCF.blue沒有任何經驗,因此我嘗試使用標准WCF客戶端和服務器來創建示例應用程序來演示工作方案。 也許它會幫助您找到缺少的連接以使您的場景工作。

我使用了BusinessRuleFaultExceptionTypeCodeReason屬性。 BusinessRuleFaultExceptionType是WCF錯誤契約。

我有點懶,並在一個控制台應用程序中實現了所有代碼。 Wcf客戶端使用與Wcf服務相同的Datacontracts和ICalculator接口。 對不起代碼。 這將是一個很長的帖子。

首先是Datacontracts和服務接口

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

[ServiceContract(Namespace = "http://UE.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract(IsOneWay = false)]
    [FaultContract(typeof(BusinessRuleFaultExceptionType))]
    double Add(double n1, double n2);

    [OperationContract(IsOneWay = false)]
    [FaultContract(typeof(BusinessRuleFaultExceptionType))]
    double Subtract(double n1, double n2);

    [OperationContract(IsOneWay = false)]
    [FaultContract(typeof(BusinessRuleFaultExceptionType))]
    double Multiply(double n1, double n2);

    [OperationContract(IsOneWay = false)]
    [FaultContract(typeof(BusinessRuleFaultExceptionType))]
    double Divide(double n1, double n2);
}

/// <summary>
/// General fault structure. 
/// </summary>
[DataContract(Namespace = "http://someurl.temp")]
public sealed class BusinessRuleFaultExceptionType
{
    [DataMember]
    public int Code { get; set; }

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

現在,服務實現:

[ErrorBehavior(typeof(MyErrorHandler))]
public class CalculatorService : ICalculator
{
    public double Add(double n1, double n2)
    {
        double result = n1 + n2;
        Console.WriteLine("Received Add({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        throw new ArgumentException("My exception");
        return result;
    }

    public double Subtract(double n1, double n2)
    {
        double result = n1 - n2;
        Console.WriteLine("Received Subtract({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }

    public double Multiply(double n1, double n2)
    {
        double result = n1 * n2;
        Console.WriteLine("Received Multiply({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }

    public double Divide(double n1, double n2)
    {
        double result = n1 / n2;
        Console.WriteLine("Received Divide({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }
}

和客戶端實施:

public class Client : ClientBase<ICalculator>, ICalculator
{

    public double Add(double n1, double n2)
    {
        try
        {
            return base.Channel.Add(n1, n2);
        }
        catch (FaultException<BusinessRuleFaultExceptionType> ex)
        {
            Console.WriteLine("This is my Code: {0}. This is the reason: {1}", ex.Detail.Code, ex.Detail.Reason);
        }
        catch (Exception ex)
        {
            throw;
        }
        return 0;
    }

    public double Subtract(double n1, double n2)
    {
        throw new NotImplementedException();
    }

    public double Multiply(double n1, double n2)
    {
        throw new NotImplementedException();
    }

    public double Divide(double n1, double n2)
    {
        throw new NotImplementedException();
    }
}

主要程序來演示這個例子

internal class Program
{
    private static void Main(string[] args)
    {
        ServiceHost myServiceHost = new ServiceHost(typeof(CalculatorService));

        // Open the ServiceHostBase to create listeners and start listening for messages.
        myServiceHost.Open();

        // The service can now be accessed.
        Console.WriteLine("The service is ready.");
        Console.WriteLine("Press <ENTER> to terminate service.");
        Console.WriteLine();

        Console.ReadLine();

        Console.WriteLine("Sending data from client to service.");
        Client c = new Client();
        var res = c.Add(1, 2);

        Console.ReadLine();
    }

}

錯誤處理的實現:

/// <summary>
/// Helper class for exception repackaging.
/// </summary>
internal class MyExceptionHandler
{
    /// <summary>
    /// Handles thrown exception into internal exceptions that are being sent over to client.
    /// </summary>
    /// <param name="error">Exception thrown.</param>
    /// <returns>Repackaged exception.</returns>
    internal static Exception HandleError(Exception error)
    {
        // could do something here.
        return error;
    }
}

#region Behaviour
/// <summary>
/// Control the fault message returned to the caller and optionally perform custom error processing such as logging.
/// </summary>
public sealed class MyErrorHandler : IErrorHandler
{
    /// <summary>
    /// Provide a fault. The Message fault parameter can be replaced, or set to null to suppress reporting a fault.
    /// </summary>
    /// <param name="error">The <see cref="Exception"/> object thrown in the course of the service operation.</param>
    /// <param name="version">The SOAP version of the message.</param>
    /// <param name="fault">The <see cref="System.ServiceModel.Channels.Message"/> object that is returned to the client, or service, in the duplex case.</param>
    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        //If it's a FaultException already, then we have nothing to do
        if (error is FaultException)
            return;

        error = MyExceptionHandler.HandleError(error);

        var serviceDebug = OperationContext.Current.EndpointDispatcher.ChannelDispatcher.IncludeExceptionDetailInFaults;

        BusinessRuleFaultExceptionType f = new BusinessRuleFaultExceptionType
        {
            Code = -100,
            Reason = "xxx"
        };

        FaultException<BusinessRuleFaultExceptionType> faultException = new FaultException<BusinessRuleFaultExceptionType>(f, error.Message);
        MessageFault faultMessage = faultException.CreateMessageFault();
        fault = Message.CreateMessage(version, faultMessage, faultException.Action);
    }

    /// <summary>
    /// Enables error-related processing and returns a value that indicates whether the dispatcher aborts the session and the instance context in certain cases.
    /// </summary>
    /// <param name="error">The exception thrown during processing.</param>
    /// <returns>true if Windows Communication Foundation (WCF) should not abort the session (if there is one) and instance context if the instance context is not Single; otherwise, false. The default is false.</returns>
    public bool HandleError(Exception error)
    {
        // could use some logger like Nlog but as an example it will do.
        Console.WriteLine("Error occured. {0}", error);

        return true;
    }
}

/// <summary>
/// This attribute is used to install a custom error handler for a service
/// </summary>
public sealed class ErrorBehaviorAttribute : Attribute, IServiceBehavior
{
    /// <summary>
    /// Type of component to which this error handled should be bound
    /// </summary>
    private readonly Type errorHandlerType;

    /// <summary>
    /// Initializes a new instance of the ErrorBehaviorAttribute class.
    /// </summary>
    /// <param name="errorHandlerType">Type of component to which this error handled should be bound</param>
    public ErrorBehaviorAttribute(Type errorHandlerType)
    {
        this.errorHandlerType = errorHandlerType;
    }

    /// <summary>
    /// Type of component to which this error handled should be bound
    /// </summary>
    public Type ErrorHandlerType
    {
        get { return errorHandlerType; }
    }

    /// <summary>
    /// Provides the ability to inspect the service host and the service description to confirm that the service can run successfully.
    /// </summary>
    /// <param name="description">
    /// <para>Type: <see cref="System.ServiceModel.Description.ServiceDescription"/></para>
    /// <para>The service description.</para>
    /// </param>
    /// <param name="serviceHostBase">
    /// <para>Type: <see cref="System.ServiceModel.ServiceHostBase"/></para>
    /// <para>The service host that is currently being constructed.</para>
    /// </param>
    void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
    {
    }

    /// <summary>
    /// Provides the ability to pass custom data to binding elements to support the contract implementation.
    /// </summary>
    /// <param name="description">
    /// <para>Type: <see cref="System.ServiceModel.Description.ServiceDescription"/></para>
    /// <para>The service description.</para>
    /// </param>
    /// <param name="serviceHostBase">
    /// <para>Type: <see cref="System.ServiceModel.ServiceHostBase"/></para>
    /// <para>The host of the service.</para>
    /// </param>
    /// <param name="endpoints">
    /// <para>Type: <see cref="System.Collections.ObjectModel.Collection&lt;ServiceEndpoint&gt;"/></para>
    /// <para>The service endpoints.</para>
    /// </param>
    /// <param name="parameters">
    /// <para>Type: <see cref="System.ServiceModel.Channels.BindingParameterCollection"/></para>
    /// <para>Custom objects to which binding elements have access.</para>
    /// </param>
    void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
    {
    }

    /// <summary>
    /// Provides the ability to change run-time property values or insert custom extension objects such as error handlers, message or parameter interceptors, security extensions, and other custom extension objects.
    /// </summary>
    /// <param name="description">
    /// <para>Type: <see cref="System.ServiceModel.Description.ServiceDescription"/></para>
    /// <para>The service description.</para>
    /// </param>
    /// <param name="serviceHostBase">
    /// <para>Type: <see cref="System.ServiceModel.ServiceHostBase"/></para>
    /// <para>The host that is currently being built.</para>
    /// </param>
    void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
    {
        IErrorHandler errorHandler;

        try
        {
            errorHandler = (IErrorHandler)Activator.CreateInstance(errorHandlerType);
        }
        catch (MissingMethodException e)
        {
            throw new ArgumentException("The errorHandlerType specified in the ErrorBehaviorAttribute constructor must have a public empty constructor.", e);
        }
        catch (InvalidCastException e)
        {
            throw new ArgumentException("The errorHandlerType specified in the ErrorBehaviorAttribute constructor must implement System.ServiceModel.Dispatcher.IErrorHandler.", e);
        }

        foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
        {
            ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
            channelDispatcher.ErrorHandlers.Add(errorHandler);
        }
    }
}

#endregion

和我的控制台應用程序的app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <client>
      <endpoint address="http://localhost:12345/service/calc" binding="basicHttpBinding" contract="ConsoleApplication2.ICalculator" >
      </endpoint>
    </client>
    <services>
      <service name="ConsoleApplication2.CalculatorService" behaviorConfiguration="service">
        <endpoint address="http://localhost:12345/service/calc" binding="basicHttpBinding" contract="ConsoleApplication2.ICalculator" >
        </endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:12345/service/calc" />
          </baseAddresses>
        </host>
      </service>

    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="service">
          <serviceMetadata httpGetEnabled="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

我使用WCF測試客戶端發送此請求:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://UE.ServiceModel.Samples/ICalculator/Add</Action>
  </s:Header>
  <s:Body>
    <Add xmlns="http://UE.ServiceModel.Samples">
      <n1>0</n1>
      <n2>1</n2>
    </Add>
  </s:Body>
</s:Envelope>

得到了這個回應:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <s:Fault>
      <faultcode>s:Client</faultcode>
      <faultstring xml:lang="sk-SK">My exception</faultstring>
      <detail>
        <BusinessRuleFaultExceptionType xmlns="http://someurl.temp" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
          <Code>-100</Code>
          <Reason>xxx</Reason>
        </BusinessRuleFaultExceptionType>
      </detail>
    </s:Fault>
  </s:Body>
</s:Envelope>

我打電話的時候

Client c = new Client();
var res = c.Add(1, 2);

我捕獲了FaultException<BusinessRuleFaultExceptionType> ex ,我登錄到控制台

Console.WriteLine("This is my Code: {0}. This is the reason: {1}", ex.Detail.Code, ex.Detail.Reason);

編輯:我在BusinessRuleFaultExceptionType更改了名稱空間,並將解決方案設置為使用[XmlSerializerFormat(SupportFaults = true)]

更改了界面,數據交換和服務實現:

[ServiceContract(Namespace = "http://UE.ServiceModel.Samples")]
[XmlSerializerFormat(SupportFaults = true)]
public interface ICalculator
{
    [OperationContract(IsOneWay = false)]
    [FaultContract(typeof(BusinessRuleFaultExceptionType))]
    double Add(double n1, double n2);

    [OperationContract(IsOneWay = false)]
    [FaultContract(typeof(BusinessRuleFaultExceptionType))]
    double Subtract(double n1, double n2);

    [OperationContract(IsOneWay = false)]
    [FaultContract(typeof(BusinessRuleFaultExceptionType))]
    double Multiply(double n1, double n2);

    [OperationContract(IsOneWay = false)]
    [FaultContract(typeof(BusinessRuleFaultExceptionType))]
    double Divide(double n1, double n2);
}

/// <summary>
/// General fault structure. 
/// </summary>
[DataContract(Name = "BusinessRuleFaultExceptionType", Namespace = "http://someurl.temp")]
[XmlType("BusinessRuleFaultExceptionType", Namespace = "http://someurl.temp")]
public sealed class BusinessRuleFaultExceptionType
{
    //[DataMember]
    [XmlElement(IsNullable = false,Namespace = "http://namespaces2.url")]
    public int Code { get; set; }

    [XmlElement(IsNullable = false, Namespace = "http://namespaces2.url")]
    public string Reason { get; set; }
}

[ErrorBehavior(typeof(MyErrorHandler))]
public class CalculatorService : ICalculator
{
    public double Add(double n1, double n2)
    {
        double result = n1 + n2;
        Console.WriteLine("Received Add({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);

        throw new FaultException<BusinessRuleFaultExceptionType>(new BusinessRuleFaultExceptionType()
        {
            Code = -100,
            Reason = "xxx"
        });
        //throw new ArgumentException("My exception");
        return result;
    }

    public double Subtract(double n1, double n2)
    {
        double result = n1 - n2;
        Console.WriteLine("Received Subtract({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }

    public double Multiply(double n1, double n2)
    {
        double result = n1 * n2;
        Console.WriteLine("Received Multiply({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }

    public double Divide(double n1, double n2)
    {
        double result = n1 / n2;
        Console.WriteLine("Received Divide({0},{1})", n1, n2);
        Console.WriteLine("Return: {0}", result);
        return result;
    }
}

我發現了一篇文章,說明在IErrorHandler中使用XmlSerializer時出現問題的原因。 因此,我已經更改了服務實現,以在方法實現中拋出FaultException,並且不依賴於IErrorHandler。

我還發現另一篇(相對較舊的)文章如何在IErroHandler中使用XmlSerializer ,經過一段時間我甚至可以從IErrorHandler進行序列化。 我將trown異常更改回ArgumentException。 以下是更改(我繼續前面的示例,因此可能不需要所有代碼和屬性):

[DataContract(Name = "BusinessRuleFaultExceptionType", Namespace = "http://someurl.temp")]
[XmlType("BusinessRuleFaultExceptionType", Namespace = "http://someurl.temp")]
[XmlRoot("BusinessRuleFaultExceptionType", Namespace = "http://someurl.temp")]
public sealed class BusinessRuleFaultExceptionType
{
    //[DataMember]
    [XmlElement(IsNullable = false, Namespace = "http://namespaces2.url")]
    public int Code { get; set; }

    [XmlElement(IsNullable = false, Namespace = "http://namespaces2.url")]
    public string Reason { get; set; }
}

public class XmlSerializerMessageFault : MessageFault
{
    FaultCode code;
    FaultReason reason;

    object details;

    public XmlSerializerMessageFault(FaultCode code, FaultReason reason, object details)
    {
        this.details = details;
        this.code = code;
        this.reason = reason;
    }

    public override FaultCode Code
    {
        get { return code; }
    }

    public override bool HasDetail
    {
        get { return (details != null); }
    }

    protected override void OnWriteDetailContents(System.Xml.XmlDictionaryWriter writer)
    {
        var ser = new XmlSerializer(details.GetType());
        ser.Serialize(writer, details);
        writer.Flush();
    }

    public override FaultReason Reason
    {
        get { return reason; }
    }
}

/// <summary>
/// Control the fault message returned to the caller and optionally perform custom error processing such as logging.
/// </summary>
public sealed class MyErrorHandler : IErrorHandler
{
    /// <summary>
    /// Provide a fault. The Message fault parameter can be replaced, or set to null to suppress reporting a fault.
    /// </summary>
    /// <param name="error">The <see cref="Exception"/> object thrown in the course of the service operation.</param>
    /// <param name="version">The SOAP version of the message.</param>
    /// <param name="fault">The <see cref="System.ServiceModel.Channels.Message"/> object that is returned to the client, or service, in the duplex case.</param>
    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {

        BusinessRuleFaultExceptionType f = new BusinessRuleFaultExceptionType
        {
            Code = -100,
            Reason = "xxx"
        };

        // create a fault message containing our FaultContract object
        FaultException<BusinessRuleFaultExceptionType> faultException = new FaultException<BusinessRuleFaultExceptionType>(f, error.Message);
        MessageFault faultMessage = faultException.CreateMessageFault();

        var msgFault = new XmlSerializerMessageFault(faultMessage.Code, faultMessage.Reason, f);

        fault = Message.CreateMessage(version, msgFault, faultException.Action);
    }

    /// <summary>
    /// Enables error-related processing and returns a value that indicates whether the dispatcher aborts the session and the instance context in certain cases.
    /// </summary>
    /// <param name="error">The exception thrown during processing.</param>
    /// <returns>true if Windows Communication Foundation (WCF) should not abort the session (if there is one) and instance context if the instance context is not Single; otherwise, false. The default is false.</returns>
    public bool HandleError(Exception error)
    {
        // could use some logger like Nlog but as an example it will do.
        Console.WriteLine("Error occured. {0}", error);

        return true;
    }
}

在這兩種情況下,序列化故障是:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header />
  <s:Body>
    <s:Fault>
      <faultcode>s:Client</faultcode>
      <faultstring xml:lang="sk-SK">My exception</faultstring>
      <detail>
        <BusinessRuleFaultExceptionType xmlns="http://someurl.temp" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
          <Code xmlns="http://namespaces2.url">-100</Code>
          <Reason xmlns="http://namespaces2.url">xxx</Reason>
        </BusinessRuleFaultExceptionType>
      </detail>
    </s:Fault>
  </s:Body>
</s:Envelope>

我建議使用IOperationInvoker而不是IErrorHandler。 使用IOperationInvoker:

  • 您可以使用單個try / catch語句。 它將像所有操作都包含在try / catch中一樣工作。
  • 您無需手動構造生成的消息。

您案例中的實現可能如下所示:

public object Invoke(object instance, object[] inputs, out object[] outputs)
{
    try
    {
        return _childInvoker.Invoke(instance, inputs, out outputs);
    }
    catch (Exception error)
    {
        throw FaultExceptionFactory.CreateFaultException(error);
    }
}

暫無
暫無

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

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