Castle.Core.InterceptorAttribute 不注入拦截器
基于 Castle.Core.InterceptorAttribute 的文档,我试图进行这个简单的测试通过,但运气不佳:
using NUnit.Framework;
using Castle.DynamicProxy;
using Castle.Core;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
public interface IIntercepted { string get(); }
[Interceptor(typeof(TestInterceptor))]
public class Intercepted : IIntercepted
{
public virtual string get() { return "From Service"; }
}
public class TestInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
invocation.ReturnValue = "From Proxy";
}
}
[TestFixture]
public class TestFixture
{
[Test]
public void Test_interception()
{
var container = new DefaultKernel();
container.Register(
Component.For<TestInterceptor>().LifeStyle.Transient,
Component.For<IIntercepted>().ImplementedBy<Intercepted>());
var instance = container.Resolve<IIntercepted>();
Assert.That(instance.get(), Is.EqualTo("From Proxy"));
}
}
在逐步执行测试时,instance
不是代理,get()
返回“From Service”。在我看来,在这种情况下,我不需要将 get()
设为虚拟,但这样做只是为了确定。我感觉我在这里缺少一些明显和基本的东西,比如是否需要在这里注册一个设施才能使容器知道拦截器属性?我找不到任何这方面的文档。有人可以告诉我我做错了什么吗?
我使用的是 Castle 2.5 版和 .Net Framework 4.0 版。
Based on the documentation for Castle.Core.InterceptorAttribute, I am trying to make this simple test pass, and am having no luck:
using NUnit.Framework;
using Castle.DynamicProxy;
using Castle.Core;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
public interface IIntercepted { string get(); }
[Interceptor(typeof(TestInterceptor))]
public class Intercepted : IIntercepted
{
public virtual string get() { return "From Service"; }
}
public class TestInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
invocation.ReturnValue = "From Proxy";
}
}
[TestFixture]
public class TestFixture
{
[Test]
public void Test_interception()
{
var container = new DefaultKernel();
container.Register(
Component.For<TestInterceptor>().LifeStyle.Transient,
Component.For<IIntercepted>().ImplementedBy<Intercepted>());
var instance = container.Resolve<IIntercepted>();
Assert.That(instance.get(), Is.EqualTo("From Proxy"));
}
}
In stepping through the tests, instance
is not a proxy and get()
returns "From Service". It seems to me that in this case, I should not need to make get()
virtual, but did so just to be sure. I have the feeling I am missing something obvious and fundamental here, like is there a facility that needs to be registered here to make the container aware of the Interceptor attribute? I can't find any documentation to that effect. Can someone tell me what I am doing wrong?
I am using Castle version 2.5 and the 4.0 version of the .Net Framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您要直接使用
DefaultKernel
,则必须设置代理工厂:否则,只需使用
WindsorContainer
(推荐)。顺便说一句:在这种情况下,您不需要在 impl 类中将该方法设为虚拟。
If you're going to use the
DefaultKernel
directly, you have to set up the proxy factory:Otherwise, just use
WindsorContainer
instead (recommended).BTW: you don't need to make the method virtual in the impl class in this case.