Castle.Core.InterceptorAttribute 不注入拦截器

发布于 2024-09-29 20:21:26 字数 1413 浏览 7 评论 0原文

基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停滞 2024-10-06 20:21:26

如果您要直接使用 DefaultKernel,则必须设置代理工厂:

var container = new DefaultKernel {ProxyFactory = new DefaultProxyFactory()};

否则,只需使用 WindsorContainer (推荐)。

顺便说一句:在这种情况下,您不需要在 impl 类中将该方法设为虚拟。

If you're going to use the DefaultKernel directly, you have to set up the proxy factory:

var container = new DefaultKernel {ProxyFactory = new DefaultProxyFactory()};

Otherwise, just use WindsorContainer instead (recommended).

BTW: you don't need to make the method virtual in the impl class in this case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文