C# 具有Fluent接口的Castle拦截器

C# 具有Fluent接口的Castle拦截器,c#,castle-windsor,fluent-interface,iinterceptor,C#,Castle Windsor,Fluent Interface,Iinterceptor,我试图让我编写的拦截器工作,但由于某些原因,当我请求我的组件时,它似乎没有实例化拦截器。我正在做这样的事情(请原谅我,如果这不是很好的编译,但你应该明白): container.Register( Component.For().LifeStyle.Transient, AllTypes.Pick().fromsassembly(…).If(t=>typeof(IView).IsAssignableFrom(t))。 配置(c=>c.LifeStyle.Is(LifestyleType.Tran

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

container.Register(
Component.For().LifeStyle.Transient,
AllTypes.Pick().fromsassembly(…).If(t=>typeof(IView).IsAssignableFrom(t))。
配置(c=>c.LifeStyle.Is(LifestyleType.Transient).Named(…)。
拦截器(新拦截器参考(typeof(MyInterceptor))。
使用service.FromInterface(typeof(IView));
我在拦截器的构造函数中放置了断点,它似乎根本没有实例化它

在过去,我使用XML配置注册了拦截器,但我热衷于使用fluent接口


非常感谢您的帮助!

我认为您误用了服务的
。FromInterface
。文档上说:

使用实现查找子对象 接口。例如:如果您有 iSeries设备和IPProductService: 网络接口, 当您调用 FromInterface(typeof(IService))然后 将使用IPProductService。非常有用 当你想注册你所有的 服务,但不希望指定 都是

您还缺少
拦截器组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();
    }
}
[TestFixture]
公共类PPT测试{
公共接口IFoo{
无效Do();
}
公共类Foo:IFoo{
public void Do(){}
}
公共类MyInterceptor:IInterceptor{
公共无效拦截(IInvocation调用){
控制台写入线(“截获”);
}
}
[测试]
公共无效拦截器(){
var container=新的WindsorContainer();
集装箱。登记(
Component.For().LifeStyle.Transient,
AllTypes.Pick()
.From(类型(Foo))
.If(t=>typeof(IFoo).IsAssignableFrom(t))
.Configure(c=>c.LifeStyle.Is(LifestyleType.Transient)
.Interceptors(新的InterceptorReference(typeof(MyInterceptor))).Anywhere)
.WithService.Select(new[]{typeof(IFoo)});
container.Resolve().Do();
}
}

我认为您误用了
和service.FromInterface
。文档上说:

使用实现查找子对象 接口。例如:如果您有 iSeries设备和IPProductService: 网络接口, 当您调用 FromInterface(typeof(IService))然后 将使用IPProductService。非常有用 当你想注册你所有的 服务,但不希望指定 都是

您还缺少
拦截器组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();
    }
}
[TestFixture]
公共类PPT测试{
公共接口IFoo{
无效Do();
}
公共类Foo:IFoo{
public void Do(){}
}
公共类MyInterceptor:IInterceptor{
公共无效拦截(IInvocation调用){
控制台写入线(“截获”);
}
}
[测试]
公共无效拦截器(){
var container=新的WindsorContainer();
集装箱。登记(
Component.For().LifeStyle.Transient,
AllTypes.Pick()
.From(类型(Foo))
.If(t=>typeof(IFoo).IsAssignableFrom(t))
.Configure(c=>c.LifeStyle.Is(LifestyleType.Transient)
.Interceptors(新的InterceptorReference(typeof(MyInterceptor))).Anywhere)
.WithService.Select(new[]{typeof(IFoo)});
container.Resolve().Do();
}
}

这是可行的,似乎我只是错过了InterceptorGroup上的Anywhere,我想这只是我没有阅读文档的一个例子(出于兴趣,文档在哪里?)。WithService.Select()似乎没有选择我想要的带有IView的接口,因为它已经实现了多次(不要问),FromService似乎做到了这一点。这很有效,似乎我只是错过了InterceptorGroup上的Anywhere,我想这只是我没有阅读文档的一个例子(出于兴趣,它们在哪里?)。WithService.Select()似乎没有选择我想要的带有IView的接口,因为它已经实现了多次(不要问)然而,FromService似乎做到了这一点。