DependencyResolver.SetResolver Does Not Work

I am trying to install a container in a new application using the Dependency.SetResolver method and use autofac with autofac mvc 5 integration.

The problem is that the inverter setting does not work. By default, the resolver will always be used and will always expect the default constructor.

Any ideas?

Edit - global.asax.cs , I simplified it to:

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

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

+6
source share
1 answer

You forgot to register your controllers:

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

Learn more about the Autofac wiki page.

+17
source

Source: https://habr.com/ru/post/957892/


All Articles