문제

How do you add an Exception or Logging interceptor into a Windsor Controller Factory using fluent configuration?

In my bootstrapper container I new up my installers to pass in values like assemblyName.

In my ControllerInstaller I do this, which I think is wrong and why it is not working:

container.Register(
    Classes
        .FromAssemblyNamed(_assemblyName)
        .BasedOn<IController>()
        .LifestyleTransient(),
    Component
        .For<IController>()
        .ImplementedBy<Controller>()
        .Interceptors(InterceptorReference.ForType<LoggingAspect>())
        .Anywhere);

In my loggingAspect installer I do this:

container.Register(
    Component
        .For<IInterceptor>()
        .ImplementedBy<LoggingAspect>());
도움이 되었습니까?

해결책

I think this should work...

_container.Register(
    Classes
        .FromAssemblyNamed(_assemblyName)
        .BasedOn<IController>()
        .LifestyleTransient()
        .Configure(component => component.Interceptors<LoggingAspect>()));

다른 팁

This is a nice article about creating a custom Windsor Controller Factory.
ASP.NET MVC with Windsor – programmatic controller registration

And below is an example of how to register an interceptor using fluent configuration.

Putting the two together should be easy enough. You just need to add the .Interceptors<MyLoggingInterceptor>() line to the registration of your windsor controller factory and/or to the registration of your controllers.

container.Register(
    Component
        .For<IInterceptor>()
        .ImplementedBy<MyLoggingInterceptor>(),
    Component
        .For<IInterceptor>()
        .ImplementedBy<MyArgumentInterceptor>(),
    Component
        .For<IDoStuff>()
        .ImplementedBy<DoStuff>()
        .Interceptors<MyLoggingInterceptor>()
        .Interceptors<MyArgumentInterceptor>());

Another Approach:

container.Register(
    Component.For<IInterceptor>()
        .ImplementedBy<ExceptionHandlerInterceptor>()
        .LifeStyle.Is(LifestyleType.Transient)
        .Named("ExceptionHandlerInterceptor"),
    Component.For<IController>()
        .ImplementedBy<Controller>()
        .LifeStyle.Is(LifestyleType.Transient)
        .Interceptors("ExceptionHandlerInterceptor"));
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top