문제

나는 내가 일하기 위해 작성한 인터셉터를 얻으려고 노력하고 있지만 어떤 이유로 든 내 구성 요소를 요청할 때 인터셉터를 인스턴스화하는 것은 아닙니다. 나는 이런 일을하고 있습니다 (이것이 컴파일하지 않으면 용서해 주지만 아이디어를 얻어야합니다) :

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