Solve the Problems Related to automatic injection of Autofac MVC when Areas is split into different dll versions.

Source: Internet
Author: User

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.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.