C# IErrorHandler实现不处理异常
我有一个双工WCF服务,我正试图为它创建一个全局异常处理程序 我已根据为IErrorHandler实现创建了行为扩展 如果我在ApplyDispatchBehavior方法上设置断点,我可以看到我的处理程序被添加到channel dispatchers ErrorHandlers属性中,但是如果我随后抛出异常,则不会调用HandleError或ProviderDefault方法 我的错误处理程序如下所示:C# IErrorHandler实现不处理异常,c#,.net,wcf,ierrorhandler,C#,.net,Wcf,Ierrorhandler,我有一个双工WCF服务,我正试图为它创建一个全局异常处理程序 我已根据为IErrorHandler实现创建了行为扩展 如果我在ApplyDispatchBehavior方法上设置断点,我可以看到我的处理程序被添加到channel dispatchers ErrorHandlers属性中,但是如果我随后抛出异常,则不会调用HandleError或ProviderDefault方法 我的错误处理程序如下所示: internal class ControlFrameworkErrorHandler :
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();
}
}
<?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>
我的App.config如下所示:
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();
}
}
<?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>
您应该将
HandleError
方法中的代码移动到providdefault
方法。出于某种原因,尽管文档中说应该使用HandleError
,但事实似乎并非如此。我和你有同样的问题,我用providdefault
方法解决了这个问题
我在互联网上也读到其他一些人提出同样的建议
如果其他人有更好的主意,我洗耳恭听。您应该将
HandleError
方法中的代码移动到providdefault
方法。出于某种原因,尽管文档中说应该使用HandleError
,但事实似乎并非如此。我和你有同样的问题,我用providdefault
方法解决了这个问题
我在互联网上也读到其他一些人提出同样的建议
如果其他人有更好的主意,我洗耳恭听。您可能可以更改
channelDispatcher.ErrorHandlers.Add(errorHandler)
到channelDispatcher.ErrorHandlers.Add(此)代码>从外观上看,因为您的行为元素已经创建了错误处理程序的实例,并且将其添加到channeldispatchers的代码正在错误处理程序中运行。您可能可以更改channelDispatcher.errorHandler.Add(errorHandler)
到channelDispatcher.ErrorHandlers.Add(此)代码>,因为您的行为元素已经创建了错误处理程序的实例,并且将其添加到channeldispatchers的代码正在错误处理程序中运行。