autofac是比较简单易用的IOC容器。下面我们展示如何通过json配置文件,来进行控制反转。

需要用到以下程序集。可以通过nugget分别安装

Microsoft.Extensions.Configuration.dll

Microsoft.Extensions.Configuration.Json

Autofac.Configuration.dll

注意,项目目标框架最好设置为.NET Framework 4.6.1及以上。因为Microsoft.Extensions.Configuration.dll,依赖.NETStandard2.0

下表列出了 .NET Standard 的所有版本及其支持的平台

AutofacExt帮助类

using Autofac;
using Autofac.Configuration;
using Microsoft.Extensions.Configuration; namespace autofacConsole
{
public static class AutofacExt
{
private static IContainer _container; public static void InitAutofac()
{ // Add the configuration to the ConfigurationBuilder.
var config = new ConfigurationBuilder();
config.AddJsonFile("autofac.json"); // Register the ConfigurationModule with Autofac.
var module = new ConfigurationModule(config.Build());
var builder = new ContainerBuilder();
builder.RegisterModule(module); // Set the dependency resolver to be Autofac.
_container = builder.Build(); } /// <summary>
/// 从容器中获取对象
/// </summary>
/// <typeparam name="T"></typeparam>
public static T GetFromFac<T>()
{
return _container.Resolve<T>();
// return (T)DependencyResolver.Current.GetService(typeof(T));
} public static T GetFromFac<T>(string name)
{
return _container.ResolveNamed<T>(name);
}
}
}

客户端调用

public interface IOutput
{
void Write(string content);
}
public class ConsoleOutput : IOutput
{
public void Write(string content)
{
Console.WriteLine(content);
}
} class Program
{
static void Main(string[] args)
{
AutofacExt.InitAutofac();
var writer =AutofacExt.GetFromFac<IOutput>();
writer.WriteDate();
Console.ReadKey();
}
}

json配置文件配置

Autofac.json

{
"defaultAssembly": "autofacConsole",
"components": [
{
"type": "autofacConsole.ConsoleOutput, autofacConsole",
"services": [
{
"type": "autofacConsole.IOutput,autofacConsole"
}
],
"instanceScope": "single-instance",
"injectProperties": true
}
]
}

设置为如果较新则复制

参考资料:

https://github.com/autofac/Autofac

https://autofac.readthedocs.io/en/latest/getting-started/index.html

https://autofac.readthedocs.io/en/latest/configuration/xml.html

Autofac通过配置的方式的更多相关文章

  1. 通过配置的方式Autofac 《第三篇》

    一.基本配置 1.通过配置的方式使用Autofac <?xml version="1.0"?> <configuration> <configSect ...

  2. Spring声明式事务(xml配置事务方式)

    Spring声明式事务(xml配置事务方式) >>>>>>>>>>>>>>>>>>>& ...

  3. STM8S---IO复用配置(STVP方式)

    1 说明 STM8S的IO复用用程序代码配置起来比較麻烦.通常是操作flash来操作option byte字节.配置寄存器更加麻烦,能够使用STM 标准外设驱动库来设置. 本文使用一种界面配置的方式来 ...

  4. net 中web.config单一解决方法 (其他配置引入方式)

    近期一个项目需要写许多的配置项,发现在单个web.config里面写的话会很乱也难于查找 所以搜了一下解决了,记录下来 一.   webconfig提供了引入其他config的方式 <conne ...

  5. JAVA配置&注解方式搭建简单的SpringMVC前后台交互系统

    前面两篇文章介绍了 基于XML方式搭建SpringMVC前后台交互系统的方法,博文链接如下: http://www.cnblogs.com/hunterCecil/p/8252060.html htt ...

  6. virtualbox下centos虚拟机安装,并网卡配置桥接方式上网,使得和host可以互Ping通。

    见:http://www.cnblogs.com/taoshiqian/p/7615993.html 注意: 1.host 主机什么都不要处理 2.将virtualbox 的对应虚拟机网络设置桥接 3 ...

  7. Spring实现Ioc的多种方式--控制反转、依赖注入、xml配置的方式实现IoC、对象作用域

    Spring实现Ioc的多种方式 一.IoC基础 1.1.概念: 1.IoC 控制反转(Inversion of Control) IoC是一种设计思想. 2.DI 依赖注入 依赖注入是实现IoC的一 ...

  8. Spring中,使用Java配置的方式进行依赖注入

    之前用spring的时候,只知道使用xml的方式,和使用注解的方式,却一直不知道在spring中,还可以使用Java类的方式进行配置.使用Java类的方式,就可以取代xml和注解.使用Java配置是S ...

  9. tomcat下配置https方式

    [本地HTTPS]①.<Connector SSLEnabled="true" clientAuth="false" keystoreFile=" ...

随机推荐

  1. 西湖论剑2019--一道MISC题目的解题思路

    TTL题的writeup 第一次打西湖论剑,啥都不懂,被题目虐的很惨,一共就做出来两道题,但也算有收获.这里分享一下TTL那道misc题目的writeup,算是给自己点安慰吧. 题目描述 我们截获了一 ...

  2. vue 动态渲染数据很慢或不渲染

    vue 动态渲染数据很慢或不渲染 原因是因为vue检测速度很慢,因为多层循环了,在VUE 2.x的时候还能渲染出来,1.x的时候压根渲染不出来.解决方式:在动态改变数据的方法,第一行加上 this.$ ...

  3. 封装带SSH跳板机的REDIS

    一.封装ssh的redis 二.setting的配置 三.应用示例 import redis from sshtunnel import SSHTunnelForwarder from conf.se ...

  4. [java]将秒数转化为“天时分秒”的格式(转贴+修改)

    public class Time { // format seconds to day hour minute seconds style // Exmplae 5000s will be form ...

  5. 前端知识点回顾——Javascript篇(六)

    fetch 在原生ajax+es6promise的基础上封装的一个语法糖,返回promise对象. fetch(url, initObj) .then(res=>res.json()) .the ...

  6. 实体类(VO,DO,DTO)的划分

    实体类(VO,DO,DTO)的划分 (2011-12-21 15:50:27) 转载▼ 标签: it   经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实际应用情况两个角度 ...

  7. css sprite 雪碧图

    使用雪碧图的目的:有时为了美观,我们会使用一张图片来代替一些小图标,但是一个网页可能有很多很多的小图标,浏览器在显示页面的时候,就需要像服务器发送很多次访问请求, 这样一来,一是造成资源浪费,二是会导 ...

  8. [Ubuntu]18安装百度网盘

     1.下载客户端 下载地址: 选择linux版本,我选择的是deb格式,下载就可以了. 2.安装 进入下载目录,点击右键,选择在终端打开. 之后输入 以下代码愉快的安装就好了 注意:dpkg后面跟的文 ...

  9. nginx配置, 启动命令, 反向代理配置

    2014年1月3日 13:52:07 喜欢这样的风格,干货 http://huoding.com/2013/10/23/290 -----------------下边是我自己的经验(windows)- ...

  10. Python新利器之pipenv

    前言 之前学习异步asyncio库的时候,因为asyncio库支持Python3.5以上的版本,而我的Ubuntu14.04只有Python3.4,虽然下载了Python3.6,但是想直接利用ipyt ...