I am trying to use implement IErrorHandler to signal elmah to log unhandled exceptions.
I had multiple projects in my solution. I have a Utility project where I have implemented IErrorHandler.
public abstract class BaseWebServiceErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return false;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (error == null) return;
if (System.Web.HttpContext.Current == null)
{
ErrorLog.GetDefault(null).Log(new Error(error));
}
else
{
ErrorSignal.FromCurrentContext().Raise(error);
}
}
}
public class ServiceErrorBehaviourAttribute : Attribute, IServiceBehavior
{
Type errorHandlerType;
public ServiceErrorBehaviourAttribute(Type errorHandlerType)
{
this.errorHandlerType = errorHandlerType;
}
public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
{
}
public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection parameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
{
var errorHandler = (IErrorHandler)Activator.CreateInstance(errorHandlerType);
foreach (var channelDispatcherBase in serviceHostBase.ChannelDispatchers)
{
var channelDispatcher = channelDispatcherBase as ChannelDispatcher;
channelDispatcher?.ErrorHandlers.Add(errorHandler);
}
}
}
And in one of the WCF project I have a BaseWebService class where I am trying to use the service behavior attribute
[ServiceErrorBehaviour(typeof(WebServiceErrorHandler))]
public abstract class BaseWebService : AbstractWebService
{
public BaseWebService()
{
//Code logic
}
}
public class WebServiceErrorHandler : BaseWebServiceErrorHandler
{
}
Now with the above code when an unhandled exception occurs I am getting error that the service behavior is not matching.
but when I have the definition of IErrorHandler in my BaseWebService it self it works.
[ServiceErrorBehaviour(typeof(WebServiceErrorHandler))]
public abstract class BaseWebService : AbstractWebService
{
public BaseWebService()
{
//Codelogic
}
}
//public class WebServiceErrorHandler : BaseWebServiceErrorHandler
//{
//}
public class WebServiceErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return false;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
if (error == null) return;
if (System.Web.HttpContext.Current == null)
{
ErrorLog.GetDefault(null).Log(new Error(error));
}
else
{
ErrorSignal.FromCurrentContext().Raise(error);
}
}
}
with the above implementation it works good and gets logged in Elmah too.
Am I missing some reference in the Utility project? Appreciate your suggestions.