I am implementing IErrorHandler
in order to centralize all of the error handling for my WCF service in one place. This works fairly well:
public class ServiceErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
// ..Log..
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
// ..Provide fault..
}
}
Now, we're using Ninject to inject dependencies in the rest of the service, and I'd like to do the same here. Since WCF is constructing the objects based on my configuration, and I don't think I have any hooks into this process, I need to use property injection:
[Inject]
public ILoggingService Logger { get; set; }
However, this never seems to get injected. I tried using Ninject's MVC extensions to set ServiceErrorHandler
to allow injection like a filter, but that didn't seem to do the trick. Is there a way to make this happen?
Late answer, but you can inject dependencies into
IErrorHandler
by creating your customServiceHost
, let's sayTestServiceHost
.In your
TestServiceHost
you need to do:IErrorHandler
parameter.ErrorHandlerBehaviour
*, which needs to implement bothIServiceBehavior
andIErrorHandler
. It also must have constructor withIErrorHandler
parameter.OnStarting()
method, where you will addErrorHandlerBehaviour
to service behaviours. All behaviours must be added beforebase.OnStarting()
.*the idea came from Juval Lowy's example in book - "Programming WCF Services". More information about Faults and Error extensions you can find there.
Here is the working host console application. I don't use IoC there, just Pure DI, but you can easily resolve logger with any IoC you want:
And configuration:
Btw. Make sure you added
System.Runtime.Serialization
to your references