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"
You forgot to register your controllers:
More info on Autofac wiki page