IErrorHandler实现不处理异常

时间:2012-07-13 10:45:56

标签: c# .net wcf ierrorhandler

我有一个双工WCF服务,我试图为。

创建一个全局异常处理程序

我已根据this blog post

为IErrorHandler实现创建了行为扩展

如果我在ApplyDispatchBehavior方法上放置一个断点,我可以看到我的处理程序被添加到通道调度程序ErrorHandlers的属性中,但是如果我随后抛出异常,则不会调用HandleError或ProvideFault方法。

我的错误处理程序如下所示:

internal class ControlFrameworkErrorHandler : IErrorHandler, IServiceBehavior
{
    private static readonly ILog Logger = LogManager.GetLogger(typeof(ControlFrameworkWcfService));


    public bool HandleError(Exception error)
    {
        Logger.Fatal("An unhandled exception occurred:");
        Logger.Fatal(error.Message);
        Logger.Fatal(error.StackTrace);
        return true;
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
    }

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

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        IErrorHandler errorHandler = new ControlFrameworkErrorHandler();

        foreach (var channelDispatcherBase in serviceHostBase.ChannelDispatchers)
        {
            var channelDispatcher = channelDispatcherBase as ChannelDispatcher;
            if (channelDispatcher != null)
            {
                channelDispatcher.ErrorHandlers.Add(errorHandler);
            }
        }
    }
}

我的行为实现如下:

internal class ControlFrameworkErrorHandlerElement : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get
        {
            return typeof(ControlFrameworkErrorHandler);
        }
    }

    protected override object CreateBehavior()
    {
        return new ControlFrameworkErrorHandler();
    }
}

我的App.config看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.serviceModel>
       <services>
          <service name="ControlFrameworkService.ControlFrameworkWcfService" behaviorConfiguration="ControlFrameworkServiceBehavior">
           <host>
             <baseAddresses>
              <add baseAddress="http://localhost:8080/ControlFrameworkService/service" />
             </baseAddresses>
           </host>
        <endpoint address="" binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IControlFrameworkDuplex" contract="ControlFrameworkService.IControlFrameworkDuplex" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <wsDualHttpBinding>
        <binding name="WSDualHttpBinding_IControlFrameworkDuplex" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="24.20:31:23.6470000" sendTimeout="24.20:31:23.6470000" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="8388608" maxReceivedMessageSize="2147483647" messageEncoding="Mtom" textEncoding="utf-8" useDefaultWebProxy="true" clientBaseAddress="http://localhost:808/myClient/">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="8388608" maxBytesPerRead="8388608" maxNameTableCharCount="8388608" />
          <reliableSession ordered="true" inactivityTimeout="24.20:31:23.6470000" />
          <security mode="None">
            <message clientCredentialType="None" />
          </security>
        </binding>
      </wsDualHttpBinding>
    </bindings>
    <extensions>
      <behaviorExtensions>
        <add name="ControlFrameworkErrorHandler" type="ControlFrameworkService.ControlFrameworkErrorHandlerElement, ControlFrameworkService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ControlFrameworkServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="True" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <ControlFrameworkErrorHandler />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    </system.serviceModel>
    <system.diagnostics>
      <trace autoflush="true">
      </trace>
      <sources>
        <source name="System.ServiceModel" switchValue="Information, ActivityTracing"      propagateActivity="true">
          <listeners>
            <add name="sdt" type="System.Diagnostics.XmlWriterTraceListener" initializeData="WcfDetailTrace.svclog" />
          </listeners>
        </source>
      </sources>
    </system.diagnostics>
</configuration>

1 个答案:

答案 0 :(得分:1)

您应该将HandleError方法中的代码移至ProvideFault方法。出于某种原因,即使文档说应该使用HandleError,但似乎并非如此。我和你有同样的问题,我使用ProvideFault方法解决了这个问题。

我已经读过一些其他人在互联网上提出同样的建议。

如果其他人有更好的主意,我会全力以赴。