Autofac 使用JSON文件配置

autofac是比較簡單易用的IOC容器。下面我們展示如何通過json配置文件,來進(jìn)行控制反轉(zhuǎn)。

需要用到以下程序集??梢酝ㄟ^nugget分別安裝

Microsoft.Extensions.Configuration.dll

Microsoft.Extensions.Configuration.Json

?Autofac.Configuration.dll


注意,項(xiàng)目目標(biāo)框架最好設(shè)置為.NET Framework 4.6.1及以上。因?yàn)镸icrosoft.Extensions.Configuration.dll,依賴.NETStandard2.0?

?下表列出了 .NET Standard 的所有版本及其支持的平臺(tái)


AutofacExt幫助類

using Autofac;using Autofac.Configuration;using Microsoft.Extensions.Configuration;namespace autofacConsole

{

? ? publicstaticclass AutofacExt

? ? {

? ? ? ? privatestatic IContainer _container;

? ? ? ? publicstaticvoid InitAutofac()

? ? ? ? {

? ? ? ? ? ? // Add the configuration to the ConfigurationBuilder.varconfig =new ConfigurationBuilder();

? ? ? ? ? ? config.AddJsonFile("autofac.json");

? ? ? ? ? ? // Register the ConfigurationModule with Autofac.varmodule =new ConfigurationModule(config.Build());

? ? ? ? ? ? varbuilder =new ContainerBuilder();

? ? ? ? ? ? builder.RegisterModule(module);

? ? ? ? ? ? // Set the dependency resolver to be Autofac._container = builder.Build();

? ? ? ? }

? ? ? ? ///<summary>/// 從容器中獲取對(duì)象

? ? ? ? ///</summary>///<typeparam name="T"></typeparam>publicstaticT GetFromFac()

? ? ? ? {

? ? ? ? ? ? return_container.Resolve();

? ? ? ? ? ? //? return (T)DependencyResolver.Current.GetService(typeof(T));? ? ? ? }

? ? ? ? publicstaticT GetFromFac(string name)

? ? ? ? {

? ? ? ? ? ? return_container.ResolveNamed(name);

? ? ? ? }

? ? }

}

?客戶端調(diào)用

publicinterface IOutput

? ? {

? ? ? ? voidWrite(stringcontent);? ? }

publicclass ConsoleOutput : IOutput

? ? {

? ? ? ? publicvoidWrite(string content)

? ? ? ? {

? ? ? ? ? ? Console.WriteLine(content);

? ? ? ? }

? ? }class Program

? ? {? ? ? ?

? ? ? ? staticvoidMain(string[] args)

? ? ? ? {

? ? ? ? ? AutofacExt.InitAutofac();

? ? ? ? ? ? varwriter =AutofacExt.GetFromFac();

? ? ? ? ? ? writer.WriteDate();

? ? ? ? ? ? Console.ReadKey();

? ? ? ? }

? ? }? ?


?json配置文件配置

Autofac.json?

{

? "defaultAssembly": "autofacConsole",

? "components": [

? ? {

? ? ? "type": "autofacConsole.ConsoleOutput, autofacConsole",

? ? ? "services": [

? ? ? ? {

? ? ? ? ? "type": "autofacConsole.IOutput,autofacConsole"? ? ? ? }

? ? ? ],

? ? ? "instanceScope": "single-instance",

? ? ? "injectProperties":true? ? }

? ]

}

設(shè)置為如果較新則復(fù)制

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,996評(píng)論 0 0
  • =========================================================...
    _燈火闌珊處閱讀 2,545評(píng)論 0 3
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,934評(píng)論 0 13
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些閱讀 2,156評(píng)論 0 2
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,872評(píng)論 0 10

友情鏈接更多精彩內(nèi)容