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:
b.RegisterControllers(typeof(MvcApplication).Assembly);
More info on Autofac wiki page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With