我正在尝试使用Autofac和Autofac.Integrations.Web来注册ASP.NET MVC控制器。我目前正在使用程序集扫描来查找控制器,但其中一个需要一个我希望传入的特殊参数。以下是我试过的注册。
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
// so far I have tried
builder.Register<SpecialController>(c =>
new SpecialController(wierdParam, c.Resolve<IDependency>())
).Named<SpecialController>("controller.special")
.As<SpecialController>().As<IController>();
/* And this
builder.Register<SpecialController>(c =>
new SpecialController(url, c.Resolve<IDependency>())
);
*/
/* plus this
builder.Register<SpecialController>(c =>
new SpecialController(url, c.Resolve<IDependency>())
).Named<SpecialController>("controller.special");
*/
感谢您的帮助。
答案 0 :(得分:2)
我能够找到一个在调试器修补和修改之后工作的注册。
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.Register<IController>(c =>
new SpecialController(weirdParam, c.Resolve<IDependency>())
).Named<IController>("controller.special");
这是有效的,因为Autofac会自动将不同的控制器注册为IController
类型的隐藏命名注册。
答案 1 :(得分:0)
E.g:
builder.RegisterType<ApplicationSettingsRepository>().As<IApplicationSettingsRepository>()
.WithParameter(new TypedParameter(typeof(NameValueCollection), ConfigurationManager.AppSettings))