views:

102

answers:

1

I am trying to use Autofac and Autofac.Integrations.Web to register ASP.NET MVC controllers. I am currently using assembly scanning to find the controllers but one of them needs a special parameter that I would prefer to pass in instead. Found below are the registrations I have tried.

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");            
*/

Thank you for your help.

+1  A: 

I was able to find a registration that worked after some tinkering and mucking with the debugger.

var builder = new ContainerBuilder();            

builder.RegisterControllers(Assembly.GetExecutingAssembly());

builder.Register<IController>(c =>
    new SpecialController(wierdParam, c.Resolve<IDependency>())
).Named<IController>("controller.special");

This works because of autofac automatically registers different controllers as hidden named registrations of the type IController.

smaclell
Glad you found it, perhaps we should add an extension to simplify this kind of override- I'll take a look at it. Thanks!
Nicholas Blumhardt
Thank you for such a wonderful tool. It was phenomenally easy to get up and running. Even if you do not add this as an extension you probably could consider adding it to the MVC/Web integration wiki page.
smaclell