Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DependencyResolver.SetResolver Not working

I'm trying to set the container on a new app using the Dependency.SetResolver method and using autofac with autofac mvc 5 integration.

The problem is that setting the resolver doesn't appear to do anything. The default resolver will always be used and will always expect a default constructor.

Any ideas?

Edit - The global.asax.cs, I've simplified it down to:

var b = new ContainerBuilder();  
b.RegisterType<UserInfo>().As<IUserSession>().InstancePerHttpRequest();  
var container = b.Build();  
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

And HomeController takes an IUserSession in its constructor. The exception thrown is "No parameterless constructor defined for this object." Thrown from "System.Web.Mvc.DefaultControllerActivator.Create"

like image 340
flukus Avatar asked Nov 12 '13 09:11

flukus


1 Answers

You forgot to register your controllers:

b.RegisterControllers(typeof(MvcApplication).Assembly);

More info on Autofac wiki page

like image 75
Alexandr Nikitin Avatar answered Oct 15 '22 23:10

Alexandr Nikitin