Due to the complexity of the project business, multiple Areas are created and placed in different projects. The project uses IOC implemented by AutoFac.
Configure the code
1 public class MvcApplication: System. web. httpApplication 2 {3 protected void Application_Start () 4 {5 // dependency injection 6 var builder = new ContainerBuilder (); 7 builder. registerModule (new ConfigurationSettingsReader ("autofac"); 8 builder. registerControllers (Assembly. getExecutingAssembly (); 9 10 var container = builder. build (); 11 DependencyResolver. setResolver (new AutofacDependencyResolver (container); 12 13 AreaRegistration. registerAllAreas (); 14 WebApiConfig. register (GlobalConfiguration. configuration); 15 FilterConfig. registerGlobalFilters (GlobalFilters. filters); 16 RouteConfig. registerRoutes (RouteTable. routes); 17} 18}
Using constructor in Controllers for Injection
Namespace xxx. web. person. controllers {// <summary> // positioning module // </summary> public class LocationController: Controller {private ILocationService _ location; public LocationController (ILocationService location) {_ location = location ;}}}
Error message after startup:
[MissingMethodException: No constructor without parameters is defined for this object.] System. runtimeTypeHandle. createInstance (RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean & canBeCached, RuntimeMethodHandleInternal & ctor, Boolean & bNeedSecurityCheck) + 0 System. runtimeType. createInstanceSlow (Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) + 98 System. runtimeType. createInstanceDefaultCtor (Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) + 241 System. activator. createInstance (Type type, Boolean nonPublic) + 69 System. web. mvc. defaultControllerActivator. create (RequestContext requestContext, Type controllerType) + 67
[InvalidOperationException: An error occurred while trying to create a controller of the "xxx. Web. Person. Controllers. HomeController" type. Make sure that the controller has a public constructor without parameters.]
The error message shows that the RegisterControllers of AutoFac only injects Controllers of xxx. Web into the referenced Controller of xxx. web. Person. dll.
The solution is as follows:
Public class MvcApplication: System. web. httpApplication {protected void Application_Start () {// dependency injection var builder = new ContainerBuilder (); builder. registerModule (new ConfigurationSettingsReader ("autofac"); builder. registerControllers (Assembly. getExecutingAssembly (); // solves the Areas injection problem in different Dll var assemblies = new DirectoryInfo (HttpContext. current. server. mapPath ("~ /Bin /")). getFiles ("*. dll "). select (r => Assembly. loadFrom (r. fullName )). toArray (); builder. registerAssemblyTypes (assemblies ). where (r => r. baseType = typeof (Controller )). instancePerHttpRequest (); var container = builder. build (); DependencyResolver. setResolver (new AutofacDependencyResolver (container); AreaRegistration. registerAllAreas (); WebApiConfig. register (GlobalConfiguration. configuration); FilterConfig. registerGlobalFilters (GlobalFilters. filters); RouteConfig. registerRoutes (RouteTable. routes );}}
Recompile and run normally.