質問

私は私が仕事に書いたインターセプタを取得しようとしているが、何らかの理由で、それは私が私のコンポーネントを要求するときインターセプタをインスタンス化していないようです。私は、この(これは非常にコンパイルされない場合は、私を許していますが、アイデアを得る必要がある)のようなものをやってます:

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構成を使用して、私のインターセプタを登録しましたが、私は流暢なインターフェイスを使用することに熱心だ。

すべてのヘルプをいただければ幸いです!

役に立ちましたか?

解決

私はあなたが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();
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top