【发布时间】:2014-06-20 23:38:53
【问题描述】:
故事是这样开始的,我使用 Castle EventWiring 工具在我的类中定义事件的侦听器,它工作得很好,我曾经像这样引发事件:
if (null != BlaBlaEvent)
{
BlaBlaEvent(someData);
}
最近我遇到一个业务需求,并认为最好的解决方案可能是使用代理模式,所以我使用了 DynamicProxy,在我的 IoC 解析功能(Castle Windsor 的一个小包装器)中,我有以下代码:
T componenet = _container.Resolve<T>();
var gen = new ProxyGenerator();
if (typeof(T).IsClass)
{
return gen.CreateClassProxyWithTarget(componenet, _container.Resolve<StashInterceptor>());
}
else
{
return gen.CreateInterfaceProxyWithTarget(componenet, _container.Resolve<StashInterceptor>());
}
这也有效,它不是返回一个具体类,而是返回一个代理,该代理拦截函数调用并进行一些处理,然后将调用转发给原始的具体类方法。
现在的问题是具体类中的事件没有侦听器(即使配置说明它们有)。
不确定这是错误还是设计决定在从代理调用方法时不加载侦听器,但目前我很困惑,需要解决方案或解决方法。
有人有想法吗?
这是我在 web.config 中的城堡 xml 配置:
<castle>
<facilities>
<facility id="event.wiring" type="Castle.Facilities.EventWiring.EventWiringFacility, Castle.Facilities.EventWiring" />
</facilities>
<components>
<component id="SomeInterceptor" type="Namespace.SomeInterceptor, MyAssembly" />
<component id="SomePublisher" type="Namespace.SomePublisher, MyAssembly">
<subscribers>
<subscriber id="SomeSubscriber" event="SomeEvent" handler="OnSomeEvent" />
</subscribers>
</component>
<component id="SomeSubscriber" type="Namespace.SomeSubscriber, MyAssembly" />
</components>
【问题讨论】:
标签: c# castle-windsor castle castle-dynamicproxy