WCF异常IErrorHandler没有被调用

时间:2013-03-08 13:02:15

标签: c# wcf exception ierrorhandler

我似乎遇到了让IErrorHandler界面工作的问题。我的代码是

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

namespace WcfService3
{
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            throw new Exception("asdf");

        }
    }

    public class MyErrorHandler : IErrorHandler
    {
        public MyErrorHandler()
        {
            string Hello = "";
        }
        public bool HandleError(Exception error)
        {
            return true;
        }

        public void ProvideFault(Exception error, MessageVersion version, ref Message msg)
        {
            var vfc = new MyFault();
            var fe = new FaultException<MyFault>(vfc);
            var fault = fe.CreateMessageFault();
            msg = Message.CreateMessage(version, fault, "http://ns");
        }
    }

    public class ErrorHandlerExtension : BehaviorExtensionElement, IServiceBehavior
    {
        public override Type BehaviorType
        {
            get { return GetType(); }
        }

        protected override object CreateBehavior()
        {
            return this;
        }

        private IErrorHandler GetInstance()
        {
            return new MyErrorHandler();
        }

        void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {
        }

        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是:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
      <services>
    <service name="WcfService3.Service1">
      <endpoint address=""
                binding="basicHttpBinding"
                contract="WcfService3.IService1" />
    </service>
  </services>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
      <extensions>
    <behaviorExtensions>
      <add name="errorHandler"
            type="WcfService3.ErrorHandlerExtension, WcfService3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </behaviorExtensions>
  </extensions>

    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

我的WCF界面是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService3
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [FaultContract(typeof(MyFault))]
        string GetData(int value);
    }

    [DataContract]
    public class MyFault
    {

    }

}

我的问题是在WCF中的IErrorHandler中,如果在WCF服务调用期间有任何异常,HandlerError()函数假设首先像C#windows应用程序UnhandledException类一样被调用,那么服务应该崩溃吗?在上面的代码中,在服务调用期间,抛出异常但是在抛出异常之前没有调用我的HandlerError函数?我的目标是记录错误,WCF服务可以抛出未处理的异常并崩溃。我期望在调试期间断点将访问HandleError函数,但是该函数没有被调用,只是出现异常?

2 个答案:

答案 0 :(得分:3)

您的<errorHandler />部分中是否遗漏了behavior

<behavior>
    <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="false"/>
    <!-- HERE -->
    <errorHandler />
    <!-- HERE -->
</behavior>

完整的答案是here。加1回答。

答案 1 :(得分:0)

如果其他任何人遇到此错误,当我遇到此错误时,这是​​因为该错误正被某个LINQ表达式调用的方法引发。直到WCF尝试序列化响应之后才真正调用该方法,然后将该响应超出服务范围。 WCF不会将这些错误传递给IErrorHandler。

使用.ToList()在返回之前对列表进行材料化为我解决了这个问题。