1. public static void BuildMvcContainer()
  2. {
  3. var builder = new ContainerBuilder();
  4. var assemblys = AppDomain.CurrentDomain.GetAssemblies().ToList();
  5.  
  6. //拆分DLL后需要注册,需要注入的DLL
  7. Assembly[] asm = GetAllAssembly("*.Controllers.dll").ToArray();
  8. builder.RegisterAssemblyTypes(asm);
  9.        //读取web.config中配置的值 
  10. builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
  11.  
  12. var container = builder.Build();
  13. DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
  14. }
  1. #region 加载程序集
  2. private static List<Assembly> GetAllAssembly(string dllName)
  3. {
  4. List<string> pluginpath = FindPlugin(dllName);
  5. var list = new List<Assembly>();
  6. foreach (string filename in pluginpath)
  7. {
  8. try
  9. {
  10. string asmname = Path.GetFileNameWithoutExtension(filename);
  11. if (asmname != string.Empty)
  12. {
  13. Assembly asm = Assembly.LoadFrom(filename);
  14. list.Add(asm);
  15. }
  16. }
  17. catch (Exception ex)
  18. {
  19. Console.Write(ex.Message);
  20. }
  21. }
  22. return list;
  23. }
  24. //查找所有插件的路径
  25. private static List<string> FindPlugin(string dllName)
  26. {
  27. List<string> pluginpath = new List<string>();
  28.  
  29. string path = AppDomain.CurrentDomain.BaseDirectory;
  30. string dir = Path.Combine(path, "bin");
  31. string[] dllList = Directory.GetFiles(dir, dllName);
  32. if (dllList.Length > )
  33. {
  34. pluginpath.AddRange(dllList.Select(item => Path.Combine(dir, item.Substring(dir.Length + ))));
  35. }
  36. return pluginpath;
  37. }
  38. #endregion

上面的代码就是注册的代码,我是在MVC4使用,写完之后再Global.asax中的Application_Start调用,确保启动应用后执行

然后是web.config中配置

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

我web.config中引用一个文件,详细看我之前的博文

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <autofac>
  3. <components>
  4. <component type="Cactus.Service.TestServer, Cactus.Service" service="Cactus.IService.TestInterface,Cactus.IService" />
  5. <component type="Cactus.Service.SiteConfigService, Cactus.Service" service="Cactus.IService.ISiteConfigService,Cactus.IService" />
  6. <component type="Cactus.Service.ActionGroupServer, Cactus.Service" service="Cactus.IService.IActionGroupServer,Cactus.IService" />
  7. <component type="Cactus.Service.ActionServer, Cactus.Service" service="Cactus.IService.IActionServer,Cactus.IService" />
  8. <component type="Cactus.Service.RoleServer, Cactus.Service" service="Cactus.IService.IRoleServer,Cactus.IService" />
  9. <component type="Cactus.Service.UserServer, Cactus.Service" service="Cactus.IService.IUserServer,Cactus.IService" />
  10. </components>
  11. </autofac>

然后在autofac.config中注册服务,type是实现的server,service是接口

在MVC中的使用,根据的是构造函数注入

  1. public class AdminLoginController : Controller
  2. {
  3. public readonly IUserServer userServer;
  4. public AdminLoginController(IUserServer userServer)
  5. {
  6. this.userServer = userServer;
  7. }
  8. public ActionResult Index()
  9. {
  10. userServer.xxxxx//随便写
  11. return View();
  12. }
  13. }

属性注入可以参考这篇文章http://www.cnblogs.com/peteryu007/p/3449315.html

Autofac的注入和web.config配合的更多相关文章

  1. 结合jquery的前后端加密解密 适用于WebApi的SQL注入过滤器 Web.config中customErrors异常信息配置 ife2018 零基础学院 day 4 ife2018 零基础学院 day 3 ife 零基础学院 day 2 ife 零基础学院 day 1 - 我为什么想学前端

    在一个正常的项目中,登录注册的密码是密文传输到后台服务端的,也就是说,首先前端js对密码做处理,随后再传递到服务端,服务端解密再加密传出到数据库里面.Dotnet已经提供了RSA算法的加解密类库,我们 ...

  2. IOC注入框架——Unity中Web.Config文件的配置与调用

    Unity 应用程序块可以从 XML 配置文件中读取配置信息.配置文件可以是 Windows Forms 应用程序的 App.config 或者 ASP.NET 应用程序的 Web.config.当然 ...

  3. .net mvc web api Autofac依赖注入框架-戈多编程

    今天自己搭了一套基于三层的依赖注入mvc web api 的依赖注入框架,在此总结下相关配置 1.设置应用程序的.net Framework版本为 4.5 2.通过Nuget 安装autofac包 I ...

  4. 从零开始,搭建博客系统MVC5+EF6搭建框架(2),测试添加数据、集成Autofac依赖注入

    一.测试仓储层.业务层是否能实现对数据库表的操作 1.创建IsysUserInfoRepository接口来继承IBaseRepository父接口 namespace Wchl.WMBlog.IRe ...

  5. [Solution] 使用Autofac在MVC、Web API、WCF中实现IOC

    本来想聊一下面试过程的,1个星期面了6家,4家当场给offer,2家技术通过(1家没下文,1家复试).从中也学习到一些东西,先还是继续Coding吧. 官网:http://autofac.org/ 下 ...

  6. 使用Autofac动态注入启动Api服务

    Autofac Autofac(https://autofac.org/)是一款.NET的IOC组件,它可以和Owin, Web Api, ASP.NET MVC, .NET Core完美结合,帮助开 ...

  7. 【干货】利用MVC5+EF6搭建博客系统(二)测试添加数据、集成Autofac依赖注入

    PS:如果图片模糊,鼠标右击复制图片网址,然后在浏览器中打开即可. 一.测试仓储层.业务层是否能实现对数据库表的操作 1.在52MVCBlog.IRepository程序集下创建IsysUserInf ...

  8. Autofac依赖注入框架

    最近使用Autofac框架做项目的依赖注入,感觉挺好用的. 没有深入研究,只是拿来用用,具体可以去官网看看:https://autofac.org/. 这里只是贴一下最近项目的配置: public p ...

  9. ASP.NET MVC Autofac自动注入

    依赖注入容器有很多插件,我用过Unity和Autofac,这两个插件给我最明显的感觉就是Autofac很快,非常的快,毕竟是第三方开发的,而Unity相对而言性能比较稳定 下面附上Autofac自动注 ...

随机推荐

  1. Hive基础之Hive开启查询列名及行转列显示

    Hive默认情况下查询结果里面是只显示值: hive> select * from click_log; OK ad_101 :: ad_102 :: ad_103 :: ad_104 :: a ...

  2. WEB前端研发工程师编程能力成长之路(1)(转)

    WEB前端研发工程师编程能力成长之路(1)   [背景] 如果你是刚进入WEB前端研发领域,想试试这潭水有多深,看这篇文章吧: 如果你是做了两三年WEB产品前端研发,迷茫找不着提高之路,看这篇文章吧: ...

  3. 文件系统取证分析(第11章:NTFS概念)

    /* Skogkatt 开始翻译于2015-01-24,仅作为学习研究之用,谢绝转载. 2015-01-31更新MFT entry 属性概念. 2015-02-01翻译完成. 译注:我翻译这本书的这三 ...

  4. QTP 场景恢复– 函数调用

    创建自动化测试是为了实现无人值守下运行,但也给开发人员带来一些问题.假如你离开办公室前启动测试,想要让它通宵运行.然而,由于不可预见的错误,您的测试会在某一点停止,中断了测试结果.因此QTP中引入场景 ...

  5. DB2日常维护——REORG TABLE命令优化数据库性能

    一个完整的日常维护规范可以帮助 DBA 理顺每天需要的操作,以便更好的监控和维护数据库,保证数据库的正常.安全.高效运行,防止一些错误重复发生. 由于DB2使用CBO作为数据库的优化器,数据库对象的状 ...

  6. 免费的API接口

    有如下三个Json格式的查询天气预报接口: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/dat ...

  7. mysql数据导入

    1.windows解压 2.修改文件名,例如a.txt 3.rz 导入到 linux \data\pcode sudo su -cd /data/pcode/rm -rf *.txt 4.合并到一个文 ...

  8. ref传递

    下面通过一个排序的小栗子来分析ref传递: static void Main(string[] args) { ,,,,}; int num; Console.WriteLine("请输入您 ...

  9. 【Qt】使用QProcess调用其它程序或脚本

    大概试了一下,还是不错的,不过字符编码问题还不太好解决: 代码: #include "mainwindow.h" #include "ui_mainwindow.h&qu ...

  10. Can Live View boot up images acquired from 64bit OS evidence?

    Some said Live View could only boot up images acquired from 32bit OS evidence. I have to say that it ...