Archive

Posts Tagged ‘WCF Behavior’

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

March 26, 2013 3 comments

 

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.

IErrorHandler
  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
  5.  
  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.     }
  15.  
  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.         }
  22.  
  23.     }
  24.  
  25.     //IServiceBehavior implementation"
  26.  
  27.     void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase host)
  28.     {
  29.         //attach the object implementing the IErrorHandler interface(this class itself) to each ChannelDisp
  30.  
  31.         foreach (ChannelDispatcher dispatcher in host.ChannelDispatchers)
  32.         {
  33.             dispatcher.ErrorHandlers.Add(this);
  34.         }
  35.     }
  36.  
  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.     }
  46.  
  47.  
  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)

    {

        dispatcher.ErrorHandlers.Add(this);

    }

}

 

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.     }

 

Thanks

Ganesh D

Advertisements