Autofac的注入和web.config配合
public static void BuildMvcContainer()
{
var builder = new ContainerBuilder();
var assemblys = AppDomain.CurrentDomain.GetAssemblies().ToList(); //拆分DLL后需要注册,需要注入的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 > )
{
pluginpath.AddRange(dllList.Select(item => Path.Combine(dir, item.Substring(dir.Length + ))));
}
return pluginpath;
}
#endregion
上面的代码就是注册的代码,我是在MVC4使用,写完之后再Global.asax中的Application_Start调用,确保启动应用后执行
然后是web.config中配置
<autofac configSource="Configuration\autofac.config" />
我web.config中引用一个文件,详细看我之前的博文
<?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中注册服务,type是实现的server,service是接口
在MVC中的使用,根据的是构造函数注入
public class AdminLoginController : Controller
{
public readonly IUserServer userServer;
public AdminLoginController(IUserServer userServer)
{
this.userServer = userServer;
}
public ActionResult Index()
{
userServer.xxxxx//随便写
return View();
}
}
属性注入可以参考这篇文章http://www.cnblogs.com/peteryu007/p/3449315.html
Autofac的注入和web.config配合的更多相关文章
- 结合jquery的前后端加密解密 适用于WebApi的SQL注入过滤器 Web.config中customErrors异常信息配置 ife2018 零基础学院 day 4 ife2018 零基础学院 day 3 ife 零基础学院 day 2 ife 零基础学院 day 1 - 我为什么想学前端
在一个正常的项目中,登录注册的密码是密文传输到后台服务端的,也就是说,首先前端js对密码做处理,随后再传递到服务端,服务端解密再加密传出到数据库里面.Dotnet已经提供了RSA算法的加解密类库,我们 ...
- IOC注入框架——Unity中Web.Config文件的配置与调用
Unity 应用程序块可以从 XML 配置文件中读取配置信息.配置文件可以是 Windows Forms 应用程序的 App.config 或者 ASP.NET 应用程序的 Web.config.当然 ...
- .net mvc web api Autofac依赖注入框架-戈多编程
今天自己搭了一套基于三层的依赖注入mvc web api 的依赖注入框架,在此总结下相关配置 1.设置应用程序的.net Framework版本为 4.5 2.通过Nuget 安装autofac包 I ...
- 从零开始,搭建博客系统MVC5+EF6搭建框架(2),测试添加数据、集成Autofac依赖注入
一.测试仓储层.业务层是否能实现对数据库表的操作 1.创建IsysUserInfoRepository接口来继承IBaseRepository父接口 namespace Wchl.WMBlog.IRe ...
- [Solution] 使用Autofac在MVC、Web API、WCF中实现IOC
本来想聊一下面试过程的,1个星期面了6家,4家当场给offer,2家技术通过(1家没下文,1家复试).从中也学习到一些东西,先还是继续Coding吧. 官网:http://autofac.org/ 下 ...
- 使用Autofac动态注入启动Api服务
Autofac Autofac(https://autofac.org/)是一款.NET的IOC组件,它可以和Owin, Web Api, ASP.NET MVC, .NET Core完美结合,帮助开 ...
- 【干货】利用MVC5+EF6搭建博客系统(二)测试添加数据、集成Autofac依赖注入
PS:如果图片模糊,鼠标右击复制图片网址,然后在浏览器中打开即可. 一.测试仓储层.业务层是否能实现对数据库表的操作 1.在52MVCBlog.IRepository程序集下创建IsysUserInf ...
- Autofac依赖注入框架
最近使用Autofac框架做项目的依赖注入,感觉挺好用的. 没有深入研究,只是拿来用用,具体可以去官网看看:https://autofac.org/. 这里只是贴一下最近项目的配置: public p ...
- ASP.NET MVC Autofac自动注入
依赖注入容器有很多插件,我用过Unity和Autofac,这两个插件给我最明显的感觉就是Autofac很快,非常的快,毕竟是第三方开发的,而Unity相对而言性能比较稳定 下面附上Autofac自动注 ...
随机推荐
- 【LeetCode】6. ZigZag Conversion 锯齿形转换
题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...
- COM学习(三)——数据类型
上回书介绍了GUID.CLSID.IID和接口的概念.本回的重点是介绍 COM 中的数据类型.咋还不介绍组件程序的设计步骤呀?咳......别着急,别着急!孔子曰:"饭要一口一口地吃&quo ...
- CSS3之圆角
CSS3圆角border-radius也是比较常用的,有了圆角,可以少很多图片了:) 语法: border-radius : none | <length>{1,4} [/ <len ...
- android屏幕基础知识
首先,先来上一张图,看看android屏幕分辨率的占比情况 什么是dp,dip,dpi,sp.px ?之间的关系是什么? px:构成图像的最小单位 dp/dip:密度无关像素 以160dpi为基准 1 ...
- Lotus防病毒与数据备份案例
Lotus防病毒与数据备份案例 上文(http://chenguang.blog.51cto.com/350944/1334595)中我们已安装好了Domino服务器,这节里我们需要考虑安全解决方案, ...
- 【WCF 1】WCF框架宏观了解
导读:使用WCF框架爱开发项目也有很长一段时间了,最开始的时候,是理解的不深,所以不写博客进行总结.后来是项目赶,发现需要总结的有很多,一直没有把WCF排上日程,尤其是最近研究EF这一块,更是研究了一 ...
- CLRS: online maximum (n,k)algorithm
//the first k elements interviewed and rejected, //for the latter n-k elements ,if value >max,re ...
- Delphi Form的释放和隐藏:free,hide,close
form.Free - 释放Form占用的所有资源.Free后,Form指针不能再使用,除非对Form重新赋值. form.Hide - 隐藏Form.可以调用form.Show再 ...
- 在CentOS 7上安装Python3.5源码包
最近开始系统学习Python 3.5,发现CentOS 7系统自带的python版本是Python 2.7.现在要使用Python 3.5该怎么办?方法大体跟安装其他程序一样.以下为详细经过: 1.事 ...
- HBase优化
1.hbase的balance策略是region数量策略,即维持每个regionserver的region数量基本一致,这并未考虑一个table的region可能都落到一个refionserver的不 ...