gpt4 book ai didi

c# - CaSTLe DynamicProxy 中断 EventWiring 监听器

转载 作者:行者123 更新时间:2023-11-30 15:29:12 25 4
gpt4 key购买 nike

故事是这样开始的,我使用 CaSTLe EventWiring 工具为我的类中的事件定义监听器,它运行良好,我过去常常引发这样的事件:

if (null != BlaBlaEvent)
{
BlaBlaEvent(someData);
}

最近我遇到了一个业务需求,我认为最好的解决方案可能是使用代理模式,所以我在我的 IoC 解析函数(CaSTLe Windsor 的一个小包装器)中使用了 DynamicProxy,我有以下代码:

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 中的 caSTLe 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>

最佳答案

注意:问题不在于 xml 或流畅的配置,而是您自己通过 CaSTLe.DynamicProxy 生成代理这一事实。如果您改为利用容器并使用容器中的功能(顺便使用 DynamicProxy)注册拦截器,这将起作用。

不确定您到底想做什么,但如下所示,向监听器注册拦截器并让它拦截监听器方法调用并同时使用事件连接功能不是问题。这是温莎 3.3

public class EventWiringFacilityTests
{
public void RegisterInterceptorWithListenerAndMakeSureListenerSubscribes()
{
var container = new WindsorContainer();

container.AddFacility<EventWiringFacility>();

container.Register(Component.For<SomeInterceptor>());

container.Register(
Component.For<SimplePublisher>()
.PublishEvent(p => p.Event += null,
x => x.To<SimpleListener>("foo", l => l.OnEvent(null, null))),
Component.For<SimpleListener>().Interceptors<SomeInterceptor>().Named("foo"));

var someInterceptor = container.Resolve<SomeInterceptor>();

var simpleListener = container.Resolve<SimpleListener>();
Assert.That(simpleListener.EventHasHappened, Is.False);

var simplePublisher = container.Resolve<SimplePublisher>();
simplePublisher.Trigger();

Assert.That(simpleListener.EventHasHappened);

simpleListener.Snap();
Assert.That(someInterceptor.Intercepted);
}
}

public class SomeInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Intercepted = true;
invocation.Proceed();
}

public bool Intercepted { get; set; }
}

public class SimplePublisher
{
public event EventHandler Event;

public void Trigger()
{
if (Event != null)
{
Event(this, new EventArgs());
}
}
}

public class SimpleListener
{
public bool EventHasHappened { get; set; }

public void OnEvent(object sender, EventArgs e)
{
EventHasHappened = true;
}

public virtual void Snap()
{

}
}

关于c# - CaSTLe DynamicProxy 中断 EventWiring 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24274358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com