var builder = new ContainerBuilder();
var container = builder.Build();

var assemblies = new DirectoryInfo(
AppDomain.CurrentDomain.BaseDirectory)
.GetFiles("ServiceLeyer.dll")
.Select(r => Assembly.LoadFrom(r.FullName)).ToArray();
builder.RegisterAssemblyTypes(assemblies);
//有拦截器
builder1.RegisterAssemblyTypes(assemblies).As<ICategoryService>().EnableInterfaceInterceptors().InterceptedBy(typeof(CallLog));
builder1.RegisterAssemblyTypes(assemblies).EnableClassInterceptors().InterceptedBy(typeof(CallLog));
//无拦截器
builder1.RegisterAssemblyTypes(assemblies).As<ICategoryService>();
builder1.RegisterAssemblyTypes(assemblies);

builder1.RegisterType<CallLog>();

builder1.Update(container);

//SingleInstance()单例使用。

//动态更新容器里的对象。

var builder1 = new ContainerBuilder();
builder1.RegisterType<CategoryService>().As <ICategoryService>()
.EnableClassInterceptors()
.InterceptedBy(typeof(CallLog));
builder1.Update(container);

//MVC注入
var builder = new ContainerBuilder();

// Register your MVC controllers.
builder.RegisterControllers(typeof(MvcApplication).Assembly);

// OPTIONAL: Register model binders that require DI.
builder.RegisterModelBinders(Assembly.GetExecutingAssembly());
builder.RegisterModelBinderProvider();

// OPTIONAL: Register web abstractions like HttpContextBase.
builder.RegisterModule<AutofacWebTypesModule>();

// OPTIONAL: Enable property injection in view pages.
builder.RegisterSource(new ViewRegistrationSource());

// OPTIONAL: Enable property injection into action filters.
builder.RegisterFilterProvider();

// Set the dependency resolver to be Autofac.
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

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 二.使用到的 ...

  10. autofac 组件的实例范围

    实例范围决定如何在请求之间共享服务. 原文地址:http://docs.autofac.org/en/latest/lifetime/instance-scope.html 每个依赖一个实例 使用这个 ...

随机推荐

  1. 强大的Sublime编辑器

    Sublime是一款功能非常强大的轻量级的代码及文本编辑工具,有关它的介绍和下载可以从官网http://www.sublimetext.com获取.尽管Sublime并非是一款完全免费的IDE开发工具 ...

  2. 我也说百度和google

    对于程序员,最好的老师恐怕还是百度或者google或一些专业的it社区.网站了罢! 之前曾听到这样的一句话, 文艺程序员用Google Scholar/Scirus/stackoverflow.普通程 ...

  3. redis常用命令、常见错误、配置技巧等分享

    转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/117.html?1455860236 1. redis查看当前所有的key ...

  4. CSS的样式表基本概念

    一.样式表分类 1.内联样式表 <p style="fint-size:24px;">直接在标签内部进行样式设置</style> 2.内嵌样式表 <h ...

  5. EF架构~XMLRepository仓储的实现~续(XAttribute方式)

    回到目录 之前我写过关于XMLRepository仓储的实现的文章,主要是针对XElement方式的,对于XML的结构,一般来说有两种,一是使用节点方式的,我习惯称为XElement方式,别一种是属性 ...

  6. iOS---类方法(静态方法)和实例方法

    类方法   实例方法是以+开头的方法, 实例方法是用实例对象访问:   类方法的对象是类而不是实例,通常用来创建对象或者工具类.     在实例方法里,根据继承原理发送消息给self和super其实都 ...

  7. iOS9和Xcode7

    2015.06.08苹果放出了iOS9以及Xcode7的 Beta测试版本.有一句话非常引入注意: https://developer.apple.com/xcode/ Now everyone ca ...

  8. 初了解JS设计模式,学习笔记

    什么是设计模式. 回答这个问题,往往我们得先知道我们为什么需要设计模式,正是因为有需求才会有设计模式,难道不是吗? 我们为什么需要设计模式. 如果没有按照设计模式去写,你的代码很可能是乱无肆忌写的,也 ...

  9. codeforces——Little Pony and Expected Maximum

    /* 我们枚举每次选择最大数值的情况:m个数, 投掷n次 最大值是1: 1种 2: 2^n-1 3: 3^n-2^n ..... m: m^n-(m-1)^n 所以最后的结果=sum((k/m)^n ...

  10. 【原创】.NET读写Excel工具Spire.Xls使用(2)Excel文件的控制

                  本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p/4288836.html .NET读写Excel工具Spire.Xls使用文章 ...