Autofac json configuration

Posted 千面佛缘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Autofac json configuration相关的知识,希望对你有一定的参考价值。

 static void Main(string[] args)
        {
            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);
            var container = builder.Build();
            IOperation operation = container.Resolve<IOperation>();
            operation.Hello();

           
        }
{
  "defaultAssembly": "Autofac.Example.Calculator",
  "components": [
    {
      "type": "Autofac.Example.Calculator.Addition.Add, Autofac.Example.Calculator",
      "services": [
        {
          "type": "Auofac.Example.Calculator.Api.IOperation,Autofac.Example.Calculator"
        }
      ],
      "injectProperties": true
    },
    {
      "type": "Autofac.Example.Calculator.Division.Divide, Autofac.Example.Calculator",
      "services": [
        {
          "type": "Auofac.Example.Calculator.Api.IOperation,Autofac.Example.Calculator"
        }
      ],
      "parameters": {
        "places": 4
      }
    }
  ]
}
public class IOperation
   {
       public virtual void Hello()
       {
           Console.WriteLine("Hello");
       }
   }
 
 public class Divide : IOperation
   {
       public override void Hello()
       {
           Console.WriteLine("Hello,I‘m Divide");
       }
   }
 
public class Add : IOperation
   {
       public override void Hello()
       {
           Console.WriteLine("Hello,I‘m Add");
       }
   }
<?xml version="1.0" encoding="utf-8" ?>
<autofac defaultAssembly="Autofac.Example.Calculator">
    <components name="0">
        <type>Autofac.Example.Calculator.Addition.Add, Autofac.Example.Calculator.Addition</type>
        <services name="0" type="Autofac.Example.Calculator.Api.IOperation" />
        <injectProperties>true</injectProperties>
    </components>
    <components name="1">
        <type>Autofac.Example.Calculator.Division.Divide, Autofac.Example.Calculator.Division</type>
        <services name="0" type="Autofac.Example.Calculator.Api.IOperation" />
        <injectProperties>true</injectProperties>
        <parameters>
            <places>4</places>
        </parameters>
    </components>
</autofac>

  

  

  

  

  

以上是关于Autofac json configuration的主要内容,如果未能解决你的问题,请参考以下文章

IOC容器-Autofac在MVC中实现json方式注入使用

[Asp.Net Core]Autofac支持配置文件

[Asp.Net Core]Autofac支持配置文件

最强的 C# Autofac学习笔记

WebAPI 如何以 JSON 格式填充空结果

Autofac和nopcommerce中的Autofac, 还有反射