开发者

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

开发者 https://www.devze.com 2022-12-08 22:08 出处:网络
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.

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号