zoukankan      html  css  js  c++  java
  • .net core 集成 autofac.

    1. Install

    Install-Package Autofac
    Install-Package Autofac.Extensions.DependencyInjection
    
    

    2.Startup

    2.1 增加成員

    public IContainer ApplicationContainer { get; private set; }
    

    2.2 Startup.ConfigureServices

    返回值改為:IServiceProvider

    末尾中增加:

    //******************* autofac start ***********************
    // Create the container builder.
    var autofacBuilder = new ContainerBuilder();
    
    
    autofacBuilder.RegisterType<TCPCollectorApplicationService>().As<ITCPCollectorApplicationService>();
    autofacBuilder.Populate(services);
    this.ApplicationContainer = autofacBuilder.Build();
    
    return new AutofacServiceProvider(this.ApplicationContainer);
    //******************* autofac start ***********************
    

    3. Usage

    3.1 构造注入

    直接構造注入即可使用。

    public TodoController(IKnowledgeApplicationService knowledgeApplicationService, ITCPCollectorApplicationService tcpCollectorApplicationService, IServiceProvider serviceProvider)
    {
        KnowledgeApplicationService = knowledgeApplicationService;
        TCPCollectorApplicationService = tcpCollectorApplicationService;
    
        ServiceProvider = serviceProvider;
    }
    

    3.2 使用ServiceProvider获取。

    var tcpSvc = ServiceProvider.GetService(typeof(ITCPCollectorApplicationService)) as ITCPCollectorApplicationService;
    return Ok(tcpSvc.GetAll());
    

    Ref:官方文檔: http://docs.autofac.org/en/latest/integration/aspnetcore.html#

  • 相关阅读:
    PHP5.5新特性
    并发导论【转】
    Redis常用数据结构和操作
    git常用命令【转】
    curl 的用法指南
    pycurl模块
    单点登陆
    MySql 里的IFNULL、NULLIF和ISNULL用法
    Mysql coalesce()函数认识和用法
    python文件操作
  • 原文地址:https://www.cnblogs.com/pengzhen/p/6912823.html
Copyright © 2011-2022 走看看