[英]Castle.Core.InterceptorAttribute not injecting interceptor
Based on the documentation for Castle.Core.InterceptorAttribute , I am trying to make this simple test pass, and am having no luck: 根据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"));
}
}
In stepping through the tests, instance
is not a proxy and get()
returns "From Service". 在逐步进行测试时,
instance
不是代理,并且get()
返回“ 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. 在我看来,在这种情况下,我不需要将
get()
虚拟化,但是这样做只是为了确保。 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? 我感觉我在这里缺少一些显而易见的基本知识,例如是否需要在这里注册一个设施以使容器知道Interceptor属性? 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. 我正在使用Castle版本2.5和.Net Framework 4.0版本。
If you're going to use the DefaultKernel
directly, you have to set up the proxy factory: 如果要直接使用
DefaultKernel
,则必须设置代理工厂:
var container = new DefaultKernel {ProxyFactory = new DefaultProxyFactory()};
Otherwise, just use WindsorContainer
instead (recommended). 否则,请改用
WindsorContainer
(推荐)。
BTW: you don't need to make the method virtual in the impl class in this case. 顺便说一句:在这种情况下,您无需在impl类中将方法虚拟化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.