The simple way to do this is by creating an attribute (ServiceExceptionHandlerAttribute) class and an error handler class (ServiceExceptionHandler) as follows
----------- the attribute --------------
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
namespace Ayman.ExceptionHandling
{
public class ServiceExceptionHandlerAttribute : Attribute,IServiceBehavior
{
#region IServiceBehavior Members
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(new ServiceExceptionHandler());
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
#endregion
}
------------ the error handler -----------------
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
namespace Ayman.ExceptionHandling
{
public class ServiceExceptionHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
ValidationFault validationFault = new ValidationFault();
FaultException
MessageFault mf = fe.CreateMessageFault();
fault = Message.CreateMessage(version, mf, fe.Action);
}
}
}
And now all you need to do, is to decorate your service with this attribute as follows
using Ayman.ExceptionHandling;
namespace Ayman.MyService
{
[ServiceExceptionHandler]
public class MyService : IMyService
{
}
}