Autofac - 将属性注入 asp.net mvc 控制器

发布于 2024-11-05 05:16:37 字数 188 浏览 1 评论 0原文

我有一个基本控制器,它继承了我的所有控制器。这个基本控制器有一些属性,我想使用属性注入来注入。

我的控制器注册看起来像这样,

builder.RegisterControllers(Assembly.GetExecutingAssembly()

我不知道如何访问基类并注入属性。

I have a base controller from which inherit all my controllers. This base controller has some properties I'd like to inject the using property injection.

My controller registration looks like this

builder.RegisterControllers(Assembly.GetExecutingAssembly()

I don't know how to access the base class and inject the properties.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听不够的曲调 2024-11-12 05:16:37

这应该有效:

builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();

autofac 网站上的更多信息:http://code.google.com/p/autofac/维基/PropertyInjection

This should work:

builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();

Some more info on the autofac website: http://code.google.com/p/autofac/wiki/PropertyInjection

远山浅 2024-11-12 05:16:37

您可能需要考虑使用 Autofac 聚合服务

当您需要将一组依赖项视为一个依赖项时,聚合服务非常有用。当一个类依赖于多个构造函数注入的服务,或者具有多个属性注入的服务时,将这些服务移至单独的类中会产生更简单的 API。

一个示例是超类和子类,其中超类具有一个或多个构造函数注入的依赖项。子类通常必须继承这些依赖关系,即使它们可能只对超类有用。通过聚合服务,超类构造函数参数可以折叠为一个参数,从而减少子类中的重复性。另一个重要的副作用是子类现在与超类依赖项的更改隔离,在超类中引入新的依赖项意味着仅更改聚合服务定义。

You may want to consider using an Autofac Aggregate Service:

An aggregate service is useful when you need to treat a set of dependencies as one dependency. When a class depends on several constructor-injected services, or have several property-injected services, moving those services into a separate class yields a simpler API.

An example is super- and subclasses where the superclass have one or more constructor-injected dependencies. The subclasses must usually inherit these dependencies, even though they might only be useful to the superclass. With an aggregate service, the superclass constructor parameters can be collapsed into one parameter, reducing the repetitiveness in subclasses. Another important side effect is that subclasses are now insulated against changes in the superclass dependencies, introducing a new dependency in the superclass means only changing the aggregate service definition.

知你几分 2024-11-12 05:16:37

这对我有用:

using Autofac;
using Autofac.Integration.Web;
using Autofac.Integration.Web.Mvc;    

builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();
builder.RegisterControllers(Assembly.GetExecutingAssembly()).InjectActionInvoker();

This works for me:

using Autofac;
using Autofac.Integration.Web;
using Autofac.Integration.Web.Mvc;    

builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();
builder.RegisterControllers(Assembly.GetExecutingAssembly()).InjectActionInvoker();
陪你搞怪i 2024-11-12 05:16:37

尼克万的回答是正确的。只需确保

builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();

在注册所有依赖项之后即可。或者换句话说,就在构建容器之前。

所以最终的代码看起来像

.
.
.
builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();           

var container = builder.Build();

nickvane's answer is correct. Just make sure

builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();

is after your all dependencies are registered. Or in other words, just right before you build your container.

So final code will look like

.
.
.
builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();           

var container = builder.Build();
云醉月微眠 2024-11-12 05:16:37

还需要调用 MVC:

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

对于 Web API:

HttpConfiguration config = GlobalConfiguration.Configuration;
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

Need to call also for MVC:

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

For Web API:

HttpConfiguration config = GlobalConfiguration.Configuration;
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文