autofac JSON文件配置
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 JSON文件配置的更多相关文章
- 让EFCore更疯狂些的扩展类库(一):通过json文件配置sql语句
前言 EF通过linq和各种扩展方法,再加上实体模型,编写数据库的访问代码确实是优美.舒服,但是生成的sql不尽如意.性能低下,尤其是复杂些的逻辑关系,最终大家还是会回归自然,选择能够友好执行sql语 ...
- .NetCore获取json文件配置内容
.netcore中的数据配置及内容用了json文件代替了之前framework的xml文件,那么json中的数据该怎么获取呢?下面讲解json文件在.net core中的获取方法. 首先,新建一个.n ...
- 【ASP.NET Core快速入门】(五)命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample 可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来,我 ...
- 菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
命令行配置 我们通过vs2017创建一个控制台项目CommandLineSample 可以看到现在项目以来的是dotnet core framework 我们需要吧asp.net core引用进来 ...
- 微信小程序自学第一课:工程目录结构与.json文件配置
注册成为开发者 地址: https://mp.weixin.qq.com/cgi-bin/wx 开发者工具下载地址 https://mp.weixin.qq.com/debug/wxadoc/dev/ ...
- .net core - 配置管理 - json文件配置
Json 文件配置 public class Startup { public Startup(IHostingEnvironment env) { var builder = new Configu ...
- iis配置js支持读取json文件配置
默认情况下,iis不支持解析.json文件,这就需要我们自己在iis下配置方法一:iis配置1.点击开始菜单选择控制面板: 2.控制面板内点击管理工具,选择Internet信息服务(IIS)管理器. ...
- 微信小程序 app.json文件配置
https://developers.weixin.qq.com/miniprogram/dev/index.html 起步 https://developers.weixin.qq.com/min ...
- package.json文件配置信息
1.概述 每个项目的根目录下面,一般都有一个package.json文件,定义了这个项目所需要的各种模块,以及项目的配置信息(比如名称.版本.许可证等元数据).npm install命令根据这个配置文 ...
随机推荐
- Pycharm上python和unittest两种姿势傻傻分不清楚
前言 经常有人在群里反馈,明明代码一样的啊,为什么别人的能出报告,我的出不了报告:为什么别人运行结果跟我的不一样啊... 这种问题先检查代码,确定是一样的,那就是运行姿势不对了,一旦导入unittes ...
- servlet笔记1
Myeclipse关于Servlet项目文件的组织方面,如下: WEB-INF:这个目录名称和位置是固定的,放置在该目录下的文件或目录,对外界来说的封闭的,也就是客户端无法用http的任何方式访问到其 ...
- Java的bitmap到C
在很多情况下android程序员需用到c,bimap这个是一个java中的类,android底层有一个skbitmap类和其对应.先在我遇到了需要把java的bitmap传递到底层中进行一些操作.现在 ...
- 解决MongoDB分页查询之count查询慢的问题
一.概述 问题描述:在项目中优化动态查询分页接口时,发现count查询很慢(数据量大概30万),那如何解决这个问题呢? 解决方法:添加索引,多个查询条件可以添加复合索引 二.测试对比 1. 未加索引时 ...
- linux 同步IO: sync、fsync与fdatasync
[linux 同步IO: sync.fsync与fdatasync] 传统的UNIX实现在内核中设有缓冲区高速缓存或页面高速缓存,大多数磁盘I/O都通过缓冲进行.当将数据写入文件时,内核通常先将该数据 ...
- 【Python爬虫】听说你又闹书荒了?豆瓣读书9.0分书籍陪你过五一
说明 五一将至,又到了学习的季节.目前流行的各大书单主打的都是豆瓣8.0评分书籍,却很少有人来聊聊这9.0评分的书籍长什么样子.刚好最近学了学python爬虫,那就拿豆瓣读书来练练手. 爬虫 本来思路 ...
- mysql 求2个坐标之间的距离
CREATE DEFINER=`root`@`%` FUNCTION `f_GetDistance`(lng1 DOUBLE,lat1 DOUBLE,lng2 DOUBLE,lat2 DOUBLE) ...
- iOS7的iBeacon初步使用
iBeacon是iOS7的新增的功能,通过BLE实现室内定位,精确到厘米级别. 测试使用两台iPhone(支持BLE),一台作为iBeacon基站广播信号,代码使用官方源码AirLocate,另外一台 ...
- Mybatis 实用篇(三)参数处理
Mybatis 实用篇(三)参数处理 sql 语句中的参数 parameterType 可以省略不写. 一.参数封装 1.1 单个参数处理 public interface UserMapper { ...
- 职责链模式c#(处理车)
using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace 职责链模式{ ...