DependencyResolver.SetResolver Not working

ⅰ亾dé卋堺 提交于 2019-12-30 04:40:27

问题


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:


You forgot to register your controllers:

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

More info on Autofac wiki page



来源:https://stackoverflow.com/questions/19925290/dependencyresolver-setresolver-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!