简体   繁体   English

在WCF服务通道调度程序上安装IErrorHandler

[英]Installing an IErrorHandler on a WCF service channel dispatcher

I would like to install an implementation of IErrorHandler on a WCF service. 我想在WCF服务上安装IErrorHandler的实现。

I am currently using this code, which does not seem to do anything: 我目前正在使用此代码,该代码似乎什么也没做:

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);
}

All code examples i've seen (including in the book i am using for WCF) shows how to install an error extension by using a custom created IServiceBehavior . 我所见过的所有代码示例(包括我正在为WCF使用的书中的示例)都显示了如何使用自定义创建的IServiceBehavior安装错误扩展名。 Is this mandatory, or my approach should work as well? 这是强制性的,还是我的方法也应该起作用?

Here is how I got this to work: 这是我如何使它工作的:

Create a class the implements IServiceBehavior. 创建一个实现IServiceBehavior的类。 The service behavior will add your class that implements IErrorHandler: 服务行为将添加实现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)
    {
    }
}

Insert the behavior when setting up your host befor calling .Open(): 在设置主机时插入行为,以调用.Open():

logServiceHost.Description.Behaviors.Insert(0, new GlobalExceptionHandlerBehavior());

You should then be able to put a break point inside your ErrorHandler() method within your ServiceErrorHandler class and it should break for you. 然后,您应该能够在ServiceErrorHandler类内的ErrorHandler()方法内放置一个断点,并且该断点应该为您服务。 This required no xml configuration and is completely code driven. 这不需要xml配置,并且完全由代码驱动。

According to this article IErrorHandler instances are added via behaviors. 根据本文的介绍, IErrorHandler实例是通过行为添加的。 There is no mention of any other mechanisms such as your example. 没有提及任何其他机制,例如您的示例。

Phil, I believe Jay's answer follows the instructions on the MSDN link you've provided, he only has commented out the guts and kept the essential for this discussion. Phil,我相信Jay的回答遵循您提供的MSDN链接上的说明,他只是评论了胆量,并保留了此讨论的必要性。 He then adds/registers the ServiceBehavior (and thus also the IErrorHandler) through his final line of code. 然后,他通过最后一行代码添加/注册ServiceBehavior(以及IErrorHandler)。

His answer is a solution to the posted question if you ask me. 如果您问我,他的回答是对已发布问题的解决方案。 I've just validated it here in a minimal self hosted project. 我刚刚在一个最小的自托管项目中对此进行了验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM