DependencyResolver.SetResolver Not working

2020-02-28 07:09发布

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"

1条回答
够拽才男人
2楼-- · 2020-02-28 07:50

You forgot to register your controllers:

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

More info on Autofac wiki page

查看更多
登录 后发表回答