Home > WCF > WCF # 22 Error Handling , Error Logging , ErrorHandlerAttribute using WCF Extensions

WCF # 22 Error Handling , Error Logging , ErrorHandlerAttribute using WCF Extensions


In this article , we will see how to write the generic error handler in the WCF which is used for custom error logging and in returning the appropriate fault message to the user if any error comes while processing the user request.

IErrorHandler interface is used for error handling and logging in the WCF.

  1. public interface IErrorHandler
  2. {
  3.     bool HandleError(Exception error);
  4.     void ProvideFault(Exception error, MessageVersion version, ref Message fault);
  5. }

HandleError method logs an error.
It returns true if the error is considered as already handled other wise false will be returned and error will be handled as usual.

ProvideFault method can only be used in DUPLEX communication.
This method allows to create the custom FaultException
This method accepts the parameter error which is of type Exception , this exception is the exception occurs during the execution of a service method.


Following is the code for IErrorHandler interface Implementation.                                         

IErrorHandler Implementation
  1. [AttributeUsage(AttributeTargets.Class)]
  2. public class ErrorHandlerBehaviorAttribute : Attribute, IErrorHandler, IServiceBehavior
  3. {
  4.     //IErrorHandler Implementation
  6.     bool IErrorHandler.HandleError(Exception error)
  7.     {
  8.         try
  9.         {
  10.             //Write your code here for Logging the errors
  11.         }
  12.         catch { }
  13.         return false;
  14.     }
  16.     void IErrorHandler.ProvideFault(Exception error, MessageVersion version, ref Message fault)
  17.     {
  18.         if (OperationContext.Current != null && OperationContext.Current.IncomingMessageHeaders != null)
  19.         {
  20.             //Write code here for modifying the Fault Messages to be returned to the USER
  21.         }
  23.     }
  25.     //IServiceBehavior implementation"
  27.     void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase host)
  28.     {
  29.         //attach the object implementing the IErrorHandler interface(this class itself) to each ChannelDisp
  31.         foreach (ChannelDispatcher dispatcher in host.ChannelDispatchers)
  32.         {
  33.             dispatcher.ErrorHandlers.Add(this);
  34.         }
  35.     }
  37.     void IServiceBehavior.Validate(ServiceDescription description, ServiceHostBase host)
  38.     {
  39.         //implementation not required at present. Can be added later if required.
  40.     }
  41.     void IServiceBehavior.AddBindingParameters(ServiceDescription description, ServiceHostBase host,
  42.         Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
  43.     {
  44.         //implementation not required at present. Can be added later if required.
  45.     }
  48. }


Brief Summary for the code above

1) It has been derived from the Attribute class so that it can be used as an attribute over the service class directly.

2)It has been also derived from IServiceBehaviour interface which is used to apply this ErrorHandler behavior to the channel dispatcher in the Service

void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase host)


    //attach the object implementing the IErrorHandler interface(this class itself) to each ChannelDispatcher

    foreach (ChannelDispatcher dispatcher in host.ChannelDispatchers)






Now our ErrorHandlerBehavior attribute is ready to use on any WCF service.The benefit of creating the atttribute for this behavior is we can directly use this in the c# code on the service class as below

Add behavior to service
  1. [ErrorHandlerBehavior()]
  2.     public class Service : IService
  3.     {
  4.      //Implement your IService methods here
  5.     }



Ganesh D

  1. World Traveler
    March 27, 2013 at 5:09 pm

    There are different ways to save the application errors, some developers like to save logs in database while some in text file. Text file responds much quicker then database. To write error logs in text file please visit: http://cybarlab.blogspot.com/2013/03/write-error-log-into-file-in-c-sharp.html

    This will log all errors with date and time in text file.

    Hope it will help you
    Thanks n regard

  2. Pushkar
    May 7, 2013 at 12:36 pm

    I implemented this code but i am getting exception which about the serialization. The fault which is dunamically created is not getting serialized. Can u help me?

  3. June 23, 2013 at 10:46 pm

    Good one..

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: