简体   繁体   中英

Castle Windsor Interceptors and Registering by Convention

I'm currently using Castle Windsor to manage dependencies for my application.

I'm currently using the Classes.FromAssemblyContaining to register all components of a given type IFoo<> and would like to register an interceptor for all of these classes.

I can get the interceptor to work when I register individual components like this:

public void SetUpContainer(IWindsorContainer container)
{
    container.Register(Component.For<IFoo>()
                                .ImplementedBy<Foo>()
                                .LifestyleSingleton().Interceptors<MyIntercepter>());
}

However I would like to keep registering by convention using Classes.FromAssembly. My registration code currently looks something like this:

public void SetUpContainer(IWindsorContainer container)
{
     container.Register(Classes.FromAssemblyContaining<Foo>()
                               .BasedOn(typeof(IFoo<>))
                               .WithService.Base());
}

How would I add an interceptor for all IFoo<> 's that are registered?

You just use the Configure method to add the interceptor:

public void SetUpContainer(IWindsorContainer container)
{
    container.Register(Classes.FromAssemblyContaining<Foo>()
                              .BasedOn(typeof(IFoo<>))
                              .WithService.Base()
                              .Configure(r => r.Interceptors<FooInterceptor>()));
}

I also needed to register the interceptor:

container.Register(Component.For<FooInterceptor>());

But presumably you'd be registering this elsewhere (by convention).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM