使用Castle Windsor将依赖项注入CustomAttribute

Bon*_*nyT 15 c# asp.net-mvc dependency-injection castle-windsor action-filter

在我的ASP.Net MVC应用程序中,我实现了一个Custom ActionFilter来授权用户.

我使用CastleWindsor为所有控制器提供依赖注入,如下所示:

  protected virtual IWindsorContainer InitializeServiceLocator()
    {
        IWindsorContainer container = new WindsorContainer();
        ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container));

        container.RegisterControllers(typeof(HomeController).Assembly);
        ComponentRegistrar.AddComponentsTo(container);

        ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(container));

        return container;
    }
Run Code Online (Sandbox Code Playgroud)

在我的CustomAttribute中,我需要一个由我的所有控制器使用的依赖项,但是我无法在属性中使用基于构造函数的注入.

那么这里最干净的方法是什么?我该如何提供依赖?