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配合的更多相关文章

  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. struts2页面上如何操作字符串

    <s:if test="prodName.length()>15"><s:property value='prodName.substring(0,15)' ...

  2. NoSuchMethodError: resolveTypeArguments

    NoSuchMethodError: resolveTypeArguments——因为spring版本冲突导致,观察解压war包后lib中有几个spring.在pom中通过exclusion解决 Ht ...

  3. cocos2d-x 3.x丨搭建Android环境下的开发环境

    所需要的一些工具软件: 1.JDK  官网下载地址:http://www.oracle.com/ttechnetwork/java/javase/downloads/index.html 2.Andr ...

  4. 【测试】trunc和round的区别

    trunc是截断:round是四舍五入:下面通过一个例子具体看一下trunc和round的不同 SQL),trunc() from dual; TRUNC() TRUNC() ------------ ...

  5. Xcode去除某种类型的警告

      首先来看下当有警告时,怎么找到警告类型,在某条警告上,右键—>Reveal in Log     下面 [ ] 中间就是警告信息     去除警告信息的几种方式:   一.使用编译器提供的宏 ...

  6. java 中的原始类型与原始封装类型

    Java   提供两种不同的类型:引用类型和原始类型(或内置类型).比如:Int是java的原始数据类型,Integer是java为int提供的封装类.Java为每个原始类型提供了封装类,常见的原始与 ...

  7. 关于 mysql 2003 客户端连接报错的处理方法

    在连接到 mysql 数据库服务器时,有时会在客户端报出 2003 的错误代码,并提示: 无法连接到服务器,但服务器却可以 ping 通,可能的原因如下: 1.网络不通.检查能不能ping通. 2.防 ...

  8. SSH Secure Shell Client的傻瓜式使用方法

    说明:本记录仅是使用此软件的一种简单的操作方式,如果想深入研究,请做如下三件事: 1)到其官网了解她的前世今生 2)下载她.安装她.操作她(这一步需要不断的尝试.不断的深入.不断的探索,当然最好理论结 ...

  9. Eclipse中Jsp页面警告的解决方法小结

    恩,只要是开发人员,这样的小事情总会遇到的,对于这其中的某些警告性的错误是不影响代码的运行的,对应的功能也是能实现的,不过总给人一种不太好看的感觉!如果代码写的比较符合规范,这些问题也就自然而然的消失 ...

  10. No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...