国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Autofac的注入和web.config配合

2019-11-17 02:21:15
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Autofac的注入和web.config配合

public static void BuildMvcContainer()        {            var builder = new ContainerBuilder();            var assemblys = AppDomain.CurrentDomain.GetAssemblies().ToList();            //拆分DLL后需要注冊(cè),需要注入的DLL            Assembly[] asm = GetAllAssembly("*.Controllers.dll").ToArray();            builder.RegisterAssemblyTypes(asm);       //讀取web.config中配置的值             builder.RegisterModule(new ConfigurationSettingsReader("autofac"));                        var container = builder.Build();            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));        }
        #region 加載程序集        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;        }        //查找所有插件的路徑        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

上面的代碼就是注冊(cè)的代碼,我是在MVC4使用,寫(xiě)完之后再Global.asax中的application_Start調(diào)用,確保啟動(dòng)應(yīng)用后執(zhí)行

然后是web.config中配置

  <autofac configSource="Configuration/autofac.config" />

我web.config中引用一個(gè)文件,詳細(xì)看我之前的博文

<?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>

然后在autofac.config中注冊(cè)服務(wù),type是實(shí)現(xiàn)的server,service是接口

在MVC中的使用,根據(jù)的是構(gòu)造函數(shù)注入

public class AdminLoginController : Controller    {        public readonly IUserServer userServer;        public AdminLoginController(IUserServer userServer)        {            this.userServer = userServer;        }        public ActionResult Index()        {            userServer.xxxxx//隨便寫(xiě)            return View();        }}

屬性注入可以參考這篇文章http://www.survivalescaperooms.com/peteryu007/p/3449315.html


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 天祝| 屏山县| 德阳市| 福州市| 美姑县| 冕宁县| 太仓市| 库车县| 浏阳市| 永修县| 子洲县| 大名县| 嘉兴市| 安仁县| 海口市| 旬阳县| 南丰县| 南昌市| 南京市| 焉耆| 施秉县| 资源县| 中方县| 铜山县| 彭阳县| 沅陵县| 墨脱县| 舒城县| 昭通市| 眉山市| 彭阳县| 太和县| 安徽省| 嘉禾县| 韶关市| 余姚市| 新兴县| 扎赉特旗| 宣化县| 始兴县| 武定县|