具有流畅界面的城堡拦截器

发布于 2024-07-29 08:58:12 字数 593 浏览 8 评论 0原文

我试图让我编写的拦截器正常工作,但由于某种原因,当我请求组件时,它似乎没有实例化拦截器。 我正在做这样的事情(如果这不能完全编译,请原谅我,但你应该明白这个想法):

container.Register(
    Component.For<MyInterceptor>().LifeStyle.Transient,
    AllTypes.Pick().FromAssembly(...).If(t => typeof(IView).IsAssignableFrom(t)).
    Configure(c => c.LifeStyle.Is(LifestyleType.Transient).Named(...).
                   Interceptors(new InterceptorReference(typeof(MyInterceptor)).
    WithService.FromInterface(typeof(IView)));

我已经在拦截器的构造函数中放置了断点,但它似乎根本没有实例化它。

过去我使用 XML 配置注册了我的拦截器,但我热衷于使用流畅的界面。

任何帮助将不胜感激!

I'm trying to get an interceptor I've written to work, but for some reason it doesn't seem to be instantiating the interceptor when I request my components. I'm doing something like this (forgive me if this doesn't quite compile, but you should get the idea):

container.Register(
    Component.For<MyInterceptor>().LifeStyle.Transient,
    AllTypes.Pick().FromAssembly(...).If(t => typeof(IView).IsAssignableFrom(t)).
    Configure(c => c.LifeStyle.Is(LifestyleType.Transient).Named(...).
                   Interceptors(new InterceptorReference(typeof(MyInterceptor)).
    WithService.FromInterface(typeof(IView)));

I've put breakpoints in the constructor for the Interceptor and it doesn't seem to be instantiating it at all.

In the past I've registered my interceptors using the XML configuration, but I'm keen to use the fluent interface.

Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

寄人书 2024-08-05 08:58:12

我认为您滥用了 WithService.FromInterface。 文档说:

使用实现来查找子对象
界面。 例如:如果你有
IService 和 IProductService :
ISomeInterface、IService、
ISomeOtherInterface。 你打电话时
FromInterface(typeof(IService)) 然后
将使用 IProductService。 有用
当您想要注册所有您的
服务,但不想具体说明
所有这些。

您还缺少 InterceptorGroup Anywhere
这是一个工作示例,我对您的示例进行了尽可能少的更改以使其工作:

[TestFixture]
public class PPTests {
    public interface IFoo {
        void Do();
    }

    public class Foo : IFoo {
        public void Do() {}
    }

    public class MyInterceptor : IInterceptor {
        public void Intercept(IInvocation invocation) {
            Console.WriteLine("intercepted");
        }
    }

    [Test]
    public void Interceptor() {
        var container = new WindsorContainer();

        container.Register(
            Component.For<MyInterceptor>().LifeStyle.Transient,
            AllTypes.Pick()
                .From(typeof (Foo))
                .If(t => typeof (IFoo).IsAssignableFrom(t))
                .Configure(c => c.LifeStyle.Is(LifestyleType.Transient)
                                    .Interceptors(new InterceptorReference(typeof (MyInterceptor))).Anywhere)
                .WithService.Select(new[] {typeof(IFoo)}));

        container.Resolve<IFoo>().Do();
    }
}

I think you're misusing WithService.FromInterface. The docs say:

Uses implements to lookup the sub
interface. For example: if you have
IService and IProductService :
ISomeInterface, IService,
ISomeOtherInterface. When you call
FromInterface(typeof(IService)) then
IProductService will be used. Useful
when you want to register all your
services and but not want to specify
all of them.

You're also missing the InterceptorGroup Anywhere.
Here's a working sample, I changed it as little as possible from your sample to make it work:

[TestFixture]
public class PPTests {
    public interface IFoo {
        void Do();
    }

    public class Foo : IFoo {
        public void Do() {}
    }

    public class MyInterceptor : IInterceptor {
        public void Intercept(IInvocation invocation) {
            Console.WriteLine("intercepted");
        }
    }

    [Test]
    public void Interceptor() {
        var container = new WindsorContainer();

        container.Register(
            Component.For<MyInterceptor>().LifeStyle.Transient,
            AllTypes.Pick()
                .From(typeof (Foo))
                .If(t => typeof (IFoo).IsAssignableFrom(t))
                .Configure(c => c.LifeStyle.Is(LifestyleType.Transient)
                                    .Interceptors(new InterceptorReference(typeof (MyInterceptor))).Anywhere)
                .WithService.Select(new[] {typeof(IFoo)}));

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