【问题标题】:Error while implementing IErrorHandler - Cannot add the behavior extension实现 IErrorHandler 时出错 - 无法添加行为扩展
【发布时间】:2012-10-29 14:48:21
【问题描述】:

我是参考这两篇文章来实现IErrorHandler的:

ErrorHandler doesn't seem...

Roy Primrose's blog post

(我使用的是 .net Framework 3.5。)

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

System.Configuration.ConfigurationErrorsException:无法将行为扩展“errorHandlerExtension”添加到名为“Test.TestService.Service1Behavior”的服务行为中,因为底层行为类型未实现 IServiceBehavior 接口。 p>

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

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
  • 我这里添加了ErrorHandler类代码——我在ErrorHandlerExtension类中实现了IServiceBehavior,类似于Error Handler Doesn't seem...的帖子。
  • 你应该在ErrorHandler中实现IServiceBehavior。您也可以在链接到的帖子中看到这一点。
  • 谢谢,在 ErrorHandler 中实现 IServiceBehavior 解决了我的问题。

标签: c# wcf ierrorhandler


【解决方案1】:

如上所述,在 ErrorHandler 中实现 IServiceBehavior 而不是 ErrorHandlerExtension 解决了这个问题。

【讨论】:

    【解决方案2】:

    我遇到了完全相同的问题。原来我犯了复制+粘贴错误。如果BehaviorType 将返回typeof(ErrorHandlerExtension) 并且CreateBehavior 将返回new ErrorHandlerExtension(),您的问题 - 就像我的一样 - 也可以解决

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      相关资源
      最近更新 更多