通过配置的方式Autofac(1)
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 ...
- Spring声明式事务(xml配置事务方式)
Spring声明式事务(xml配置事务方式) >>>>>>>>>>>>>>>>>>>& ...
- STM8S---IO复用配置(STVP方式)
1 说明 STM8S的IO复用用程序代码配置起来比較麻烦.通常是操作flash来操作option byte字节.配置寄存器更加麻烦,能够使用STM 标准外设驱动库来设置. 本文使用一种界面配置的方式来 ...
- net 中web.config单一解决方法 (其他配置引入方式)
近期一个项目需要写许多的配置项,发现在单个web.config里面写的话会很乱也难于查找 所以搜了一下解决了,记录下来 一. webconfig提供了引入其他config的方式 <conne ...
- JAVA配置&注解方式搭建简单的SpringMVC前后台交互系统
前面两篇文章介绍了 基于XML方式搭建SpringMVC前后台交互系统的方法,博文链接如下: http://www.cnblogs.com/hunterCecil/p/8252060.html htt ...
- virtualbox下centos虚拟机安装,并网卡配置桥接方式上网,使得和host可以互Ping通。
见:http://www.cnblogs.com/taoshiqian/p/7615993.html 注意: 1.host 主机什么都不要处理 2.将virtualbox 的对应虚拟机网络设置桥接 3 ...
- Spring实现Ioc的多种方式--控制反转、依赖注入、xml配置的方式实现IoC、对象作用域
Spring实现Ioc的多种方式 一.IoC基础 1.1.概念: 1.IoC 控制反转(Inversion of Control) IoC是一种设计思想. 2.DI 依赖注入 依赖注入是实现IoC的一 ...
- Spring中,使用Java配置的方式进行依赖注入
之前用spring的时候,只知道使用xml的方式,和使用注解的方式,却一直不知道在spring中,还可以使用Java类的方式进行配置.使用Java类的方式,就可以取代xml和注解.使用Java配置是S ...
- tomcat下配置https方式
[本地HTTPS]①.<Connector SSLEnabled="true" clientAuth="false" keystoreFile=" ...
随机推荐
- Mysql基础知识整
web项目部署 Java项目使用的web服务器:Tomcat.weblogic.webshare.jetty Php.python使用的web服务器:nginx.apache 搭建环境过程: 部署.发 ...
- Android KeyCode
KEYCODE_UNKNOWN=0; KEYCODE_SOFT_LEFT=1; KEYCODE_SOFT_RIGHT=2; KEYCODE_HOME=3; KEYCODE_BACK=4; KEYCOD ...
- win7提示Xshell5提示缺少msvcp110.dll解决办法
下载地址: http://www.microsoft.com/zh-CN/download/details.aspx?id=30679 X86和X64的都下载下来,安装好后重启计算机,就OK了
- linux强制svn提交时必须写注释
打开hooks,然后将pre-commit.tmpl修改为pre-commit,打开pre-commit,写入如下代码: #!/bin/sh REPOS="$1" TXN=&quo ...
- Sublime Text 3中配置运行Java
1.安装JDK并配置环境变量 2.在JDK的bin目录下新建runJava.bat文件,右键选编辑,复制粘贴如下代码并保存: @echo off cd %~dp1 echo Compiling %~n ...
- 随机数生成类Random用法
一.构造方法: Random() 创建一个新的随机数生成器. Random(long seed) 使用单个 long 种子创建一个新的随机数生成器. 无参构造方 ...
- 分布式版本控制系统Git-----8.fst-forward与no fast foward
当前分支合并到另一分支时,如果没有分歧解决,就会直接移动文件指针.这个过程叫做fastforward. 举例来说,开发一直在master分支进行,但忽然有一个新的想法,于是新建了一个develop的分 ...
- img的问题
一个div的宽高比和 里面的img的宽高比是一样的 ,div img { width:100%:height:100%;} img {border:0} img{ 设置为border:none无 ...
- mysql允许远程连接授权方法
mysql数据库和apache不在同一台服务器时,需要远程连接mysql,这就要对mysql进行远程连接授权,为了安全只允许某些ip可以连接: 假如你想root用户从ip 192.168.2.12连接 ...
- javascript 中的call和apply
一.作用及应用场景 call和apply是Function的方法,他的第一个参数是this,第二个是Function的参数.call 和 apply 都是为了改变某个函数运行时的 context 即上 ...