Run a command installation reference under NuGet
Install-package autofac-version 3.1.0
Install-package Autofac.mvc4
public static void Registerautofac () { Containerbuilder builder = new Containerbuilder (); If you need to register all by default, please write this (the main parameter needs to be modified) //builder. Registercontrollers (assembly.getexecutingassembly ()); #region IOC Register area //Get base class var baseType = typeof (Idependency); Get all the types of the current application var Assmblys = AppDomain.CurrentDomain.GetAssemblies (). ToList (); Builder. Registercontrollers (Assmblys. ToArray ()); If the current application is related to the base class, register. Builder. Registerassemblytypes (Assmblys. ToArray ()) . Where (t = basetype.isassignablefrom (t) && t! = baseType) . Asimplementedinterfaces (). Instanceperdependency (); #endregion var container = Builder. Build (); Dependencyresolver.setresolver (new Autofacdependencyresolver (container)); }
AUTOFAC Automatic registration code, when the project is successfully launched, it can be changed to manual registration to speed up efficiency
Dependency injection can be implemented by invoking the auto-registration code in Global.aspx.cs
REGISTERAUTOFAC ();
Finally, the use of the constructor function in the controllers to use the online already available examples are no longer a simple environment to build success
. NET MVC AUTOFAC Foundation Environment Building