我想在WCF服务上安装IErrorHandler的实现.
我目前正在使用此代码,它似乎没有做任何事情:
logServiceHost = new ServiceHost(typeof(Logger));
logServiceHost.AddServiceEndpoint(typeof(ILogger), binding, address);
// Implementation of IErrorHandler.
var errorHandler = new ServiceErrorHandler();
logServiceHost.Open();
// Add error handler to all channel dispatchers.
foreach (ChannelDispatcher dispatcher in logServiceHost.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(errorHandler);
}
Run Code Online (Sandbox Code Playgroud)
我见过的所有代码示例(包括在我用于WCF的书中)都显示了如何使用自定义创建的IServiceBehavior来安装错误扩展.这是强制性的,还是我的方法也应该有效?
以下是我如何使用它:
创建一个实现IServiceBehavior的类.服务行为将添加实现IErrorHandler的类:
public class GlobalExceptionHandlerBehavior : IServiceBehavior
{
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 (ChannelDispatcherBase dispatcherBase in
serviceHostBase.ChannelDispatchers)
{
var channelDispatcher = dispatcherBase as ChannelDispatcher;
if (channelDispatcher != null)
channelDispatcher.ErrorHandlers.Add(new ServiceErrorHandler());
}
}
public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
}
}
Run Code Online (Sandbox Code Playgroud)
在设置主机befor调用时插入行为.Open():
logServiceHost.Description.Behaviors.Insert(0, new GlobalExceptionHandlerBehavior());
Run Code Online (Sandbox Code Playgroud)
然后,您应该能够在ServiceErrorHandler类中的ErrorHandler()方法中放置一个断点,它应该为您打破.这不需要xml配置,完全由代码驱动.
| 归档时间: |
|
| 查看次数: |
1253 次 |
| 最近记录: |