慢慢总结

基础使用样例,在 Application_Start 中直接使用

            //autofac注册
var builder = new ContainerBuilder();
//注册Controller方式一 自己指定程序集
//var assemblies = new List<Assembly> { Assembly.Load("TestEf.Web") };
//builder.RegisterControllers(assemblies.ToArray());
//注册Controller方式二 当前执行项目的程序集
builder.RegisterControllers(Assembly.GetExecutingAssembly());
//注册泛型类使用 RegisterGeneric
builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
builder.Register<IDbContext>(c => new TestEfContext()).InstancePerLifetimeScope();
//注册普通类使用 RegisterType
builder.RegisterType<ArticleService>().As<IArticleService>().InstancePerLifetimeScope();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

① 注册 Controller

方式一:

//注册Controller方式一  自己指定程序集
var assemblies = new List<Assembly> { Assembly.Load("TestEf.Web") };
builder.RegisterControllers(assemblies.ToArray());

方式二:

//注册Controller方式二  当前执行项目的程序集
builder.RegisterControllers(Assembly.GetExecutingAssembly());

② 常用接口注册方式

方式一:泛型注册使用  RegisterGeneric

builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();

方式二:普通类型使用 RegisterType

builder.RegisterType<ArticleService>().As<IArticleService>().InstancePerLifetimeScope();

③生命周期

InstancePerDependency  默认值,每次解析时都会得到一个新的实例

SingleInstance 单例 每次解析都会得到同一个实例

InstancePerLifetimeScope 同一生命周期内得到相同的实例

InstancePerMatchingLifetimeScope  同一生命周期升级版,可以指定与某个生命周期获取获取相同的实例

InstancePerRequest  根据Request请求来确定,在一个web/http/api的上下文中共享一个组件实例

Thread Scope 线程内共享,没有特定的方法,可以根据 InstancePerMatchingLifetimeScope 来实现

④使用

方式一:在静态方法中,没有构造函数来进行注册

var _articleService = DependencyResolver.Current.GetService<IArticleService>();

Autofac 使用经验的更多相关文章

  1. AutoFac在项目中的应用

    技能大全:http://www.cnblogs.com/dunitian/p/4822808.html#skill 完整Demo:https://github.com/dunitian/LoTCode ...

  2. Autofac - MVC/WebApi中的应用

    Autofac前面写了那么多篇, 其实就是为了今天这一篇, Autofac在MVC和WebApi中的应用. 一.目录结构 先看一下我的目录结构吧, 搭了个非常简单的架构, IOC(web), IBLL ...

  3. Autofac - 生命周期

    实例生命周期决定在同一个服务的每个请求的实例是如何共享的. 当请求一个服务的时候,Autofac会返回一个单例 (single instance作用域), 一个新的对象 (per lifetime作用 ...

  4. Autofac - 属性注入

    属性注入不同于通过构造函数方式传入参数. 这里是通过注入的方式, 在类创建完毕之后, 资源释放之前, 给属性赋值. 这里, 我重新弄一些类来演示这一篇吧. public class ClassA { ...

  5. Autofac 的点滴

    泛型类型的注册和使用 public interface IRepository<T> where T:class { } public interface ISchoolDetailRep ...

  6. ASP.NET Core 整合Autofac和Castle实现自动AOP拦截

    前言: 除了ASP.NETCore自带的IOC容器外,我们还可以使用其他成熟的DI框架,如Autofac,StructureMap等(笔者只用过Unity,Ninject和Castle). 1.ASP ...

  7. Autofac 的属性注入,IOC的坑

    Autofac 是一款优秀的IOC的开源工具,完美的适配.Net特性,但是有时候我们想通过属性注入的方式来获取我们注入的对象,对不起,有时候你还真是获取不到,这因为什么呢? 1.你对Autofac 不 ...

  8. Autofac 组件、服务、自动装配 《第二篇》

    一.组件 创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式: 1.类型创建RegisterType AutoFac能够通过反射检查一个类型,选择 ...

  9. 使用Adminlite + ASP.NET MVC5(C#) + Entityframework + AutoFac + AutoMapper写了个api接口文档管理系统

    一.演示: 接口查看:http://apidoc.docode.top/ 接口后台:http://apiadmin.docode.top/ 登录:administrator,123456 二.使用到的 ...

随机推荐

  1. C++利用openssl进行公钥解密

    私钥加密的部分内容,需要用公钥解密下面的实例代码,由于私钥加密后的字符串有不可打印字符,所以程序里面进行了base64,要用的时候先解dec base64 再传递给函数 进行解密 #include & ...

  2. linux物理内存与虚拟内存

    http://www.360doc.com/content/14/0123/14/14450281_347336709.shtml 1.查看内存占用情况 $ free -m -h total used ...

  3. 【Oracle】创建用户及设定权限

    ; grant create session to ufo; grant create tablespace to ufo; grant create table to ufo; grant drop ...

  4. [Java复习] 分布式锁 Zookeeper Redis

    一般实现分布式锁都有哪些方式? 使用 Redis 如何设计分布式锁?使用 Zookeeper 来设计分布式锁可以吗? 这两种分布式锁的实现方式哪种效率比较高? 1. Zookeeper 都有哪些使用场 ...

  5. osg osgUtil::LineSegmentIntersector

    #ifdef _WIN32 #include <Windows.h> #endif // _WIN32 #include <osgViewer/Viewer> #include ...

  6. Permission denied: user=dr.who, access=READ_EXECUTE, inode="/tmp":student:supergroup:drwx------权限问题

    在查看browse directory时,点击tmp,无法进入,报错:“Permission denied: user=dr.who, access=READ_EXECUTE, inode=" ...

  7. .NET C#获取当前网页地址信息

    设当前页完整地址是:http://www.qiandabao.com/aaa/bbb.aspx?id=5&name=kelli "http://"是协议名"www ...

  8. Python3安装后无法使用退格键

    解决办法 # 安装readline模块 yum -y install readline-devel # 进入Python安装目录 cd /usr/python/Python-3.7.2 # 重新安装 ...

  9. Cas(02)——部署Cas Server

    部署Cas Server Cas应用都需要有一个Cas Server.Cas Server是基于Java Servlet实现的,其要求部署在Servlet2.4以上版本的Web容器中.在此笔者将其部署 ...

  10. java中实现在线人数统计

    //java 代码public class SessionCounter implements HttpSessionListener { private static int activeSessi ...