使用 Castle Windsor 将依赖项注入 CustomAttribute [英] Injecting dependency into CustomAttribute using Castle Windsor

查看:28
本文介绍了使用 Castle Windsor 将依赖项注入 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?

推荐答案

OK - 这似乎是 使用 ASP MVC 和 Castle Windsor 将数据库注入到验证属性中 已得到回答.

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

还有 我如何使用Windsor 将依赖项注入 ActionFilterAttributes.

通读以上内容和参考文章 - 对我来说最重要的是 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.

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

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