Autofac injection works with web. config, and autofacweb. config

Source: Internet
Author: User

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

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.