实现IErrorHandler时出错-无法添加行为扩展 [英] Error while implementing IErrorHandler - Cannot add the behavior extension

查看:95
本文介绍了实现IErrorHandler时出错-无法添加行为扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将这两篇文章用作实现IErrorHandler的参考:

ErrorHandler似乎没有...

罗伊·普里姆罗斯的博客文章

(我正在使用.net Framework 3.5.)

但是,我遇到了一些麻烦-每当我尝试启动该服务时,都会收到以下错误消息:

System.Configuration.ConfigurationErrorsException:无法将行为扩展'errorHandlerExtension'添加到名为'Test.TestService.Service1Behavior'的服务行为中,因为基础行为类型未实现IServiceBehavior接口.

这是我的代码和配置文件:

ErrorhandlerExtension.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Configuration;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Collections.ObjectModel;

namespace Test.TestService
{
class ErrorHandlerExtension:BehaviorExtensionElement,IServiceBehavior
{
    public override Type BehaviorType
    {
        get { return typeof(ErrorHandler); }
    }
    protected override object CreateBehavior()
    {
        return new ErrorHandler();
    }
    void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {

    }
    private IErrorHandler GetInstance()
    {
        return new ErrorHandler();
    }
    void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        IErrorHandler errorHandlerInstance = GetInstance();
        foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
        {
            dispatcher.ErrorHandlers.Add(errorHandlerInstance);
        }
    }

    void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
        {
            if (endpoint.Contract.Name.Equals("IMetadataExchange") &&
                endpoint.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
                continue;

            foreach (OperationDescription description in endpoint.Contract.Operations)
            {
                if (description.Faults.Count == 0)
                {
                    throw new InvalidOperationException("FaultContractAttribute not found on this method");
                }
            }
        }
    }

}

}

web.config:

<system.serviceModel>  
  <extensions>
      <behaviorExtensions>
        <add name="errorHandlerExtension"
          type="Test.TestService.ErrorHandlerExtension, Test.TestService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>

    <behaviors>
      <serviceBehaviors>
        <behavior name="Test.TestService.Service1Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <errorHandlerExtension />
       </behavior>
     </serviceBehaviors>
    </behaviors>
</system.serviceModel>

每当我删除errorHandlerExtension/元素时,该服务就可以正常工作,但是只要我包含errorHandlerExtension元素(带有错误),该服务就无法启动.我是WCF的新手,很沮丧.有什么想法吗?

添加了 ErrorHandler.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Collections.ObjectModel;

namespace Test.TestService
{
public class ErrorHandler:IErrorHandler
{

    public bool HandleError(Exception error)
    {

        LogError("UnhandledError: "+ error);
        return true;
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        FaultException faultException = new FaultException("Exception message:ProvideFault");
        MessageFault messageFault = faultException.CreateMessageFault();
        fault = Message.CreateMessage(version, messageFault, faultException.Action);
    }

}
}

解决方案

就像我上面提到的,在ErrorHandler中实现IServiceBehavior而不是ErrorHandlerExtension可以解决此问题.

I was using these two articles as a reference to implement IErrorHandler:

ErrorHandler doesn't seem...

Roy Primrose's blog post

(I'm using .net Framework 3.5.)

However, I am having a bit trouble - whenever I try to start the service, I get following error:

System.Configuration.ConfigurationErrorsException: Cannot add the behavior extension 'errorHandlerExtension' to the service behavior named 'Test.TestService.Service1Behavior' because the underlying behavior type does not implement the IServiceBehavior interface.

Here are my code and config files:

ErrorhandlerExtension.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Configuration;
using System.Configuration;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Collections.ObjectModel;

namespace Test.TestService
{
class ErrorHandlerExtension:BehaviorExtensionElement,IServiceBehavior
{
    public override Type BehaviorType
    {
        get { return typeof(ErrorHandler); }
    }
    protected override object CreateBehavior()
    {
        return new ErrorHandler();
    }
    void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {

    }
    private IErrorHandler GetInstance()
    {
        return new ErrorHandler();
    }
    void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        IErrorHandler errorHandlerInstance = GetInstance();
        foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
        {
            dispatcher.ErrorHandlers.Add(errorHandlerInstance);
        }
    }

    void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
        {
            if (endpoint.Contract.Name.Equals("IMetadataExchange") &&
                endpoint.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
                continue;

            foreach (OperationDescription description in endpoint.Contract.Operations)
            {
                if (description.Faults.Count == 0)
                {
                    throw new InvalidOperationException("FaultContractAttribute not found on this method");
                }
            }
        }
    }

}

}

web.config:

<system.serviceModel>  
  <extensions>
      <behaviorExtensions>
        <add name="errorHandlerExtension"
          type="Test.TestService.ErrorHandlerExtension, Test.TestService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>

    <behaviors>
      <serviceBehaviors>
        <behavior name="Test.TestService.Service1Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <errorHandlerExtension />
       </behavior>
     </serviceBehaviors>
    </behaviors>
</system.serviceModel>

Whenever I get rid of the errorHandlerExtension / element, the service works fine, but it fails to start whenever I include errorHandlerExtension element (with the error). I'm new to WCF and quite stumped. Any ideas?

edit: added ErrorHandler.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Collections.ObjectModel;

namespace Test.TestService
{
public class ErrorHandler:IErrorHandler
{

    public bool HandleError(Exception error)
    {

        LogError("UnhandledError: "+ error);
        return true;
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        FaultException faultException = new FaultException("Exception message:ProvideFault");
        MessageFault messageFault = faultException.CreateMessageFault();
        fault = Message.CreateMessage(version, messageFault, faultException.Action);
    }

}
}

解决方案

Like I stated above, Implementing the IServiceBehavior in the ErrorHandler instead of ErrorHandlerExtension solved this problem.

这篇关于实现IErrorHandler时出错-无法添加行为扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆