How can I inject an object into an WCF IErrorHandler implementation with Castle Windsor?

Posted by Michael Johnson on Stack Overflow See other posts from Stack Overflow or by Michael Johnson
Published on 2009-10-21T18:47:52Z Indexed on 2010/04/13 11:03 UTC
Read the original article Hit count: 534

Filed under:
|
|
|

I'm developing a set of services using WCF. The application is doing dependency injection with Castle Windsor. I've added an IErrorHandler implementation that is added to services via an attribute. Everything is working thus far. The IErrorHandler object (of a class called FaultHandler is being applied properly and invoked.

Now I'm adding logging. Castle Windsor is set up to inject the logger object (an instance of IOurLogger). This is working. But when I try to add it to FaultHandler my logger is null.

The code for FaultHandler looks something like this:

class FaultHandler : IErrorHandler
{
    public IOurLogger logger { get; set; }

    public bool HandleError(Exception error)
    {
        logger.Write("Exception type {0}. Message: {1}", error.GetType(), error.Message);

        // Let WCF handle things its way. We only want to log.
        return false;
    }

    public void ProvideFault(Exception error, MessageVersion version, Message fault)
    {
    }
}

This throws it's own exception, since logger is null when HandleError() is called.

The logger is being successfully injected into the service itself and is usable there, but for some reason I can't use it in FaultHandler.

Update: Here is the relevant part of the Windsor configuration file (edited to protect the innocent):

<configuration>
  <components>
    <component id="Logger"
               service="Our.Namespace.IOurLogger, Our.Namespace"
               type="Our.Namespace.OurLogger, Our.Namespace"
    />
  </components>
</configuration>

© Stack Overflow or respective owner

Related posts about c#

Related posts about wcf