Globalne przchwytywanie wyjatków w WCF

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Collections.ObjectModel;
using System;

namespace Framework.Services
{
    public class ServiceErrorHandlerBehaviorAttribute : Attribute, IServiceBehavior
    {
        private readonly IErrorHandler errorHandler;

        public ServiceErrorHandlerBehaviorAttribute()
        {
            errorHandler = new ServiceErrorHandler();
        }

        public ServiceErrorHandlerBehaviorAttribute(IErrorHandler errorHandler)
        {
            this.errorHandler = errorHandler;
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            var channelDispatchers = serviceHostBase.ChannelDispatchers;
            var length = channelDispatchers.Count;
            for (var i = 0; i < length; ++i)
            {
                var cd = channelDispatchers[i] as ChannelDispatcher;
                if (cd != null)
                {
                    if (!cd.ErrorHandlers.Contains(errorHandler))
                    {
                        cd.ErrorHandlers.Add(errorHandler);
                    }
                }
            }
        }

        public void AddBindingParameters(
            ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection endpoints, BindingParameterCollection bindingParameters)
        {
        }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
        }
    }
}

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;

namespace Framework.Services
{
    public class ServiceErrorHandler : IErrorHandler 
    {        
        private static readonly bool SaveErrorInLog; 
        
        static ServiceErrorHandler()
        {
	  //get value from web.config
            SaveErrorInLog = true;
        } 
        
        public bool HandleError(Exception error)
        {
            if (SaveErrorInLog)
	      HandleErrorLogic(error);
            
            return !(error is FaultException);
        }

        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            if (SaveErrorInLog)
	      HandleErrorLogic(error);
            
            if (!(error is FaultException))
            {
                var messageFault = MessageFault.CreateFault(
                    new FaultCode("MessageFault.Code"), new FaultReason(error.Message), error, new NetDataContractSerializer());
                fault = Message.CreateMessage(version, messageFault, null);
            }
        }
        
        private void HandleErrorLogic(Exception error)
        {
            try
            {
                //zapisz błąd w logu
            }
            catch
            {
            }
        }         
    }
}

    [ServiceErrorHandlerBehavior]
    public class MyService : IMyService
    {
          public void DoSomething()
          {
          }
    }
Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s