Autofac通过配置的方式
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通过配置的方式的更多相关文章
- 通过配置的方式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=" ...
随机推荐
- 剑指offer35----复制复杂链表
题目: 请实现一个cloneNode方法,复制一个复杂链表. 在复杂链表中,每个结点除了有一个next指针指向下一个结点之外,还有一个random指向链表中的任意结点或者NULL. 结点的定义如下: ...
- 设置Fedora能够使用root用户登录
1. 切换到root工作环境,因为一下操作必须拥有root权限 [ha@localhost ~]$ su root密码: 2. 编辑/etc/pam.d/gdm [root@localhost ha] ...
- laravel-5.6路由命名
1.第一种:通过route路由中的as关键字来实现 1 Route::get('api/user',['as'='web.user'],'messageController@userInformati ...
- Android View的加载流程
什么是Activity? Activity是 用户操作的可视化界面:它为用户提供了一个放置视图和交互操作的窗口.采用setContentView的方法提供.因此,可以理解Activity.Window ...
- LC 871. Minimum Number of Refueling Stops 【lock, hard】
A car travels from a starting position to a destination which is target miles east of the starting p ...
- .netcore多语言解决方案
这里本文使用Microsoft.AspNetCore.Localization来实现多语言的解决方案 默认是包含这个包的,所有不需要再额外安装 首先需要注入我们需要的服务 1.在startup.cs中 ...
- [Spark News] Spark + GPU are the next generation technology
一.资源:Spark进行机器学习,支持GPU From:https://my.oschina.net/u/2306127/blog/1602291 为了使用Spark进行机器学习,支持GPU是必须的, ...
- 7. grep
grep命令 grep grep是通用正则表达式分析程序(General Regular Expression Parser)的缩写. grep命令可以在它的输入中搜索指定的字符串模式(Pattern ...
- 资深技术Leader曹乐:如何成为技术大牛
From: https://mp.weixin.qq.com/s/QaBTm_9AJC01Isr3LLR3aw 原创: 曹乐 公众号: 再成长一次 看了下面这篇文章的话,应该会有收获. 虽然排版不好, ...
- 如何使用QTP12 UFT做功能性测试(转载)
首先,按照本博客的安装教程走的,右键管理员运行 接下来点击继续,这个界面只需要勾选到web即可 点击ok,开始运行 进入到主界面之后,file新建一个测试. 可以修改路径等等 点击create之后,出 ...