【问题标题】:Castle Windsor Interceptors and Registering by Convention温莎城堡拦截器和按惯例登记
【发布时间】:2015-07-23 06:10:27
【问题描述】:

我目前正在使用 Castle Windsor 来管理我的应用程序的依赖项。

我目前正在使用Classes.FromAssemblyContaining 注册给定类型IFoo<> 的所有组件,并希望为所有这些类注册一个拦截器。

当我像这样注册单个组件时,我可以让拦截器工作:

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

但是,我想继续按照惯例使用 Classes.FromAssembly 进行注册。我的注册码目前看起来像这样:

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

如何为所有已注册的IFoo&lt;&gt; 添加拦截器?

【问题讨论】:

    标签: c# castle-windsor interceptor


    【解决方案1】:

    你只需使用Configure方法添加拦截器:

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

    我还需要注册拦截器:

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

    但大概你会在别处注册(按照惯例)。

    【讨论】:

      猜你喜欢
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多