1

我有一个双工 WCF 服务,我正在尝试为其创建一个全局异常处理程序。

根据这篇博文,我已经为 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>
4

1 回答 1

1

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

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

如果其他人有更好的主意,我会全神贯注。

于 2013-01-20T01:06:12.283 回答