依赖注入到CustomAttribute使用温莎城堡 [英] Injecting dependency into CustomAttribute using Castle Windsor

查看:287
本文介绍了依赖注入到CustomAttribute使用温莎城堡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的ASP.Net MVC应用程序,我实现了自定义ActionFilter对用户进行授权。

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

我用CastleWindsor提供依赖注入到所有控制器如下:

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

在我的CustomAttribute,我需要被所有我的控制器的依赖,但我无法根据用户构造方法注入中的一个属性。

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?

推荐答案

确定 - 这似乎是<一个副本href=\"http://stackoverflow.com/questions/986019/database-injection-into-a-validation-attribute-with-asp-mvc-and-castle-windsor\">Database注入与ASP MVC和温莎城堡已经回答了验证属性。

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

还有<一个href=\"http://stackoverflow.com/questions/553330/how-do-i-use-windsor-to-inject-dependencies-into-actionfilterattributes/553405#553405\">How我用温莎依赖注入ActionFilterAttributes 。

已经通过上面的读取,并且引用的文章 - 关键的一条对我来说是<一个href=\"http://weblogs.asp.net/psteele/archive/2009/11/04/using-windsor-to-inject-dependencies-into-asp-net-mvc-actionfilters.aspx\">http://weblogs.asp.net/psteele/archive/2009/11/04/using-windsor-to-inject-dependencies-into-asp-net-mvc-actionfilters.aspx为别人谁是有兴趣。

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆