使用Castle Windsor注入到CustomAttribute中的依赖关系 [英] Injecting dependency into CustomAttribute using Castle Windsor

查看:422
本文介绍了使用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中,我需要一个由我的所有控制器使用的依赖项,但是我无法在属性中使用基于构造函数的注入。



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

解决方案

确定 - 这似乎是使用ASP MVC和Castle Windsor将数据库注入验证属性已被回答。



另外如何使用Windsor将依赖项注入到ActionFilterAttributes中



阅读完上述内容后,引用的文章 - 对我来说,关键是 http://weblogs.asp.net/psteele/archive/2009/11/04/using-windsor-to-inject-dependencies-into-asp-net-mvc- actionfilters.aspx 为任何其他人感兴趣。


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中的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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