zoukankan      html  css  js  c++  java
  • 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>
    

      

      

      

      

      

  • 相关阅读:
    phpcms v9 更改首页
    不是技术牛人,如何拿到国内IT巨头的Offer
    超实用的PHP代码片段!
    Android 中的 Service 全面总结
    近期十大优秀jQuery插件推荐
    DIOCP之编写第一个应用程序(二)
    DIOCP之编写第一个应用程序(一)
    DIOCP之DEMO学习顺序及达到要求
    DIOCP之EchoServer分析
    DIOCP之数据接收事件
  • 原文地址:https://www.cnblogs.com/ms_senda/p/12612198.html
Copyright © 2011-2022 走看看