简体   繁体   English

如何使用Castle Windsor将对象注入到WCF IErrorHandler实现中?

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

I'm developing a set of services using WCF. 我正在使用WCF开发一组服务。 The application is doing dependency injection with Castle Windsor. 该应用程序正在与Castle Windsor进行依赖注入。 I've added an IErrorHandler implementation that is added to services via an attribute. 我添加了一个通过属性添加到服务的IErrorHandler实现。 Everything is working thus far. 到目前为止一切都在发挥作用。 The IErrorHandler object (of a class called FaultHandler is being applied properly and invoked. IErrorHandler对象(名为FaultHandler的类)正在被正确应用并被调用。

Now I'm adding logging. 现在我正在添加日志记录。 Castle Windsor is set up to inject the logger object (an instance of IOurLogger ). Castle Windsor设置为注入记录器对象( IOurLogger一个实例)。 This is working. 这很有效。 But when I try to add it to FaultHandler my logger is null. 但是当我尝试将它添加到FaultHandler我的记录器为空。

The code for FaultHandler looks something like this: FaultHandler的代码如下所示:

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. 抛出它自己的异常,因为当HandleError()时, logger为null。

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 . 记录器已成功注入服务本身并可在那里使用,但出于某种原因我不能在FaultHandler使用它。

Update : Here is the relevant part of the Windsor configuration file (edited to protect the innocent): 更新 :这是Windsor配置文件的相关部分(为保护无辜而编辑):

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

Logger is never set for this class, hence you are getting the null ref error. 永远不会为此类设置记录器,因此您将获得null ref错误。 Can you share your config file where you are setting the dependencies? 您可以在设置依赖项的地方共享配置文件吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM