Hi!

I ahve spent like 2 days trying to get some basic exception management in my WCF Webservice. But it is like the code I added isn't there because it isn't used. No errors or nothing. I added breakpoints everywhere but it isn't called, it just stopps in my service and data classes to report of the error.

Here is the error handler class:

Code:
using System;
using System.Collections.Generic;
using System.Web;
using System.ServiceModel.Dispatcher;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Configuration;
using System.Collections.ObjectModel;

    public class ServiceHostGeneralErrorHandler : IErrorHandler 
    {
        public bool HandleError(Exception error)
        {
            // TODO: Log the message
            return true;           
        }

        public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
            if (error is FaultException)
                return;

            var faultException = new FaultException("A General Error Occured");
            MessageFault messageFault = faultException.CreateMessageFault();
            fault = Message.CreateMessage(version, messageFault, null);            
        }        
    }

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

        protected override object CreateBehavior()
        {
            return this;
        }

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

        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");
                    }
                }
            }
        }
    }
And here is the snippet from the config file that is supposed to make it work


Code:
<extensions>
      <behaviorExtensions>      
          <add name="errorHandler"
                type="MyNamespace.ErrorHandlerExtension, MyNamespace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />       </behaviorExtensions>    </extensions>
I take it this is pretty basic stuff to handle errors from WCF webservice. But I have no clue why it wont work for me... something must be missing...

kind regards
Henrik