views:

263

answers:

1

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>
A: 

Logger is never set for this class, hence you are getting the null ref error. Can you share your config file where you are setting the dependencies?

karthik
I've updated the question with the relevant part of the config file.
Michael Johnson
Since the FaultHandler class does not have a constructor which takes in he Logger component, the Logger component will have to be set through a property setter in the windsor config file. Try adding the below to the FaultHandler config element in your config file. <parameters> <logger>Logger</logger> </parameters>
karthik
sorry abt the messed up indentation :(
karthik