Autofac injection works with web. config, and autofacweb. config
Public static void BuildMvcContainer () {var builder = new ContainerBuilder (); var assemblys = AppDomain. currentDomain. getAssemblies (). toList (); // register the DLL after splitting. The DLL Assembly to be injected [] asm = GetAllAssembly ("*. controllers. dll "). toArray (); builder. registerAssemblyTypes (asm); // read the web. the Value builder configured in config. registerModule (new ConfigurationSettingsReader ("autofac"); var container = builder. build (); DependencyResolver. setResolver (new AutofacDependencyResolver (container ));}
# Region loading Assembly private static List <Assembly> GetAllAssembly (string dllName) {List <string> pluginpath = FindPlugin (dllName); var list = new List <Assembly> (); foreach (string filename in pluginpath) {try {string asmname = Path. getFileNameWithoutExtension (filename); if (asmname! = String. empty) {Assembly asm = Assembly. loadFrom (filename); list. add (asm) ;}} catch (Exception ex) {Console. write (ex. message) ;}} return list;} // find the path of all plug-ins: private static List <string> FindPlugin (string dllName) {List <string> pluginpath = new List <string> (); string path = AppDomain. currentDomain. baseDirectory; string dir = Path. combine (path, "bin"); string [] dllList = Directory. getFiles (dir, dllName); if (dllList. length> 0) {pluginpath. addRange (dllList. select (item => Path. combine (dir, item. substring (dir. length + 1);} return pluginpath;} # endregion
The above code is the registered code. I used it in MVC4. After writing the code, I will call Application_Start in Global. asax to ensure that the application is executed after being started.
Then configure in web. config
<autofac configSource="Configuration\autofac.config" />
Reference a file in my web. config. For more information, see my previous blog.
<?xml version="1.0" encoding="utf-8"?><autofac> <components> <component type="Cactus.Service.TestServer, Cactus.Service" service="Cactus.IService.TestInterface,Cactus.IService" /> <component type="Cactus.Service.SiteConfigService, Cactus.Service" service="Cactus.IService.ISiteConfigService,Cactus.IService" /> <component type="Cactus.Service.ActionGroupServer, Cactus.Service" service="Cactus.IService.IActionGroupServer,Cactus.IService" /> <component type="Cactus.Service.ActionServer, Cactus.Service" service="Cactus.IService.IActionServer,Cactus.IService" /> <component type="Cactus.Service.RoleServer, Cactus.Service" service="Cactus.IService.IRoleServer,Cactus.IService" /> <component type="Cactus.Service.UserServer, Cactus.Service" service="Cactus.IService.IUserServer,Cactus.IService" /> </components></autofac>
Then, register the service in autofac. config. type is the implemented server, and service is the interface.
The use of MVC is based on constructor injection.
Public class AdminLoginController: Controller {public readonly IUserServer userServer; public AdminLoginController (IUserServer userServer) {this. userServer = userServer;} public ActionResult Index () {userServer. xxxxx // enter return View ();}}
Property injection can refer to this article http://www.cnblogs.com/peteryu007/p/3449315.html