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. H5-Mui框架——修改mui.confirm样式

    问题简述: 使用mui框架默认提示框时,感觉与整体布局不符,因此想要更改其中的样式. 首先,查了一下资料:mui.toast样式风格及位置修改教程 以下是转载过来的文章内容. ============ ...

  2. Linux设备驱动程序 之 自旋锁

    概念 自旋锁可以再不能休眠的代码中使用,比如中断处理例程:在正确使用的情况下,自旋锁通常可以提供比信号量更高的性能: 一个自旋锁是一个互斥设备,它只能由两个值,锁定和解锁:通常实现为某个整数值中的单个 ...

  3. iOS (APP)进程间8中常用通信方式总结

    1 URL Scheme 2 Keychain 3 UIPasteboard 4 UIDocumentInteractionController 5 local socket 6 AirDrop 7 ...

  4. 谷歌guava缓存

    简易缓存,可以设置时间的缓存 private static Cache<String,String> tokenCache = CacheBuilder.newBuilder().expi ...

  5. css 添加一个图标始终保持在pc端的右下角

    <div class="dialog_maxdiv" style="display:block;"> <div id="center ...

  6. ubuntu更换源的方法

    1.查看ubuntu版本的方法: 使用命令 sudo lsb_release -a 输出如下: root@localhost:/etc/apt# sudo lsb_release -aNo LSB m ...

  7. Topic与Partition

  8. Linux (Ubuntu)安装svn

    1 先查看是否已经安装了svn 如果没有安装svn则: ubuntu@ip----:~$ svn --version The program 'svn' is currently not instal ...

  9. KDE-解决.docx .xlsx .pptx文档默认由Ark打开的问题

    安装KDE后,默认的压缩解压程序变成了Ark,并且原来默认用WPS Office打开的.docx .xlsx .pptx文档,从文件管理器双击打开时,也变成了用Ark打开. 查了下网上的资料,可通过如 ...

  10. SQL -------- JDBC 查询所有记录

    package demo; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; ...