Domanda

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>());
È stato utile?

Soluzione

I think this should work...

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

Altri suggerimenti

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"));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top