使用Castle Windsor将依赖项注入CustomAttribute

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

我使用CastleWindsor为所有控制器提供dependency injection,如下所示:

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; } 

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

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

好的 – 这似乎是数据库注入到ASP MVC和Castle Windsor的validation属性的副本,已经得到了回答。

另外,我如何使用Windsor将依赖项注入ActionFilterAttributes 。

阅读完上述内容和参考文章 – 对我来说关键的一点是http://weblogs.asp.net/psteele/archive/2009/11/04/using-windsor-to-inject-dependencies-into-asp -net-mvc-actionfilters.aspx对于任何感兴趣的人。

你不能。 属性是元数据。 将行为置于其中是错误的。 放置依赖性更糟糕。

使用属性作为标记来表示要应用行为的对象,并在其他位置实现该行为。

其他地方的 MVC中,通常意味着一个自定义动作调用程序,它使用属性中的数据来提供所需的行为。