如何使用 Castle Windsor 将对象注入到 WCF IErrorHandler 实现中?
我正在使用 WCF 开发一组服务。该应用程序正在与温莎城堡进行依赖注入。我添加了一个 IErrorHandler
实现,该实现通过属性添加到服务中。到目前为止一切正常。正在正确应用并调用 IErrorHandler
对象(属于名为 FaultHandler
的类)。
现在我正在添加日志记录。Castle Windsor 设置为注入记录器对象(一个实例) IOurLogger
)。但是当我尝试将其添加到 FaultHandler
时,我的记录器为 null。
FaultHandler
的代码看起来像这样。 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)
{
}
}
这会引发它自己的异常,因为当调用 HandleError()
时,logger
为 null
记录器已成功注入服务本身并且可以在那里使用。由于某种原因我无法在 FaultHandler
中使用它
更新:这是 Windsor 配置文件的相关部分(经过编辑以保护无辜者):
<configuration>
<components>
<component id="Logger"
service="Our.Namespace.IOurLogger, Our.Namespace"
type="Our.Namespace.OurLogger, Our.Namespace"
/>
</components>
</configuration>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论