本文介绍了使用Castle Windsor注入到CustomAttribute中的依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在ASP.Net MVC应用程序中实现了一个自定义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));

返回容器;
}

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



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

解决方案

确定 - 这似乎是已被回答。



另外。



阅读完上述内容后,引用的文章 - 对我来说,关键是为任何其他人感兴趣。


In my ASP.Net MVC application I have implemented a Custom ActionFilter to Authorize users.

I use CastleWindsor to provide dependency injection into all of the controllers as follows:

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

Within my CustomAttribute, I need a dependency that is used by all of my controllers, however I am unable to user Constructor based injection in an attribute.

So what's the cleanest way out here? How can I provide the dependency?

解决方案

OK - this seems to be a duplicate of Database injection into a validation attribute with ASP MVC and Castle Windsor which has been answered.

Also How do I use Windsor to inject dependencies into ActionFilterAttributes.

Having read through the above, and the referenced articles - the key one for me is http://weblogs.asp.net/psteele/archive/2009/11/04/using-windsor-to-inject-dependencies-into-asp-net-mvc-actionfilters.aspx for anyone else who is interested.

这篇关于使用Castle Windsor注入到CustomAttribute中的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 21:45