• netcore autofac依赖注入


     1   /// <summary>
     2     /// 默认模块
     3     /// </summary>
     4     public class DefaulModule : Autofac.Module
     5     {
     6         protected override void Load(ContainerBuilder builder)
     7         {
     8             base.Load(builder);
     9             //var assemblies = new List<Assembly>
    10             //{
    11             //    typeof(IAutofacScanInfrustructure).Assembly,
    12             //    typeof(IAutofacScanBskFramework).Assembly,
    13             //    typeof(IAutofacScanCore).Assembly
    14             //};
    15 
    16             builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces().InstancePerLifetimeScope();
    17             //builder.RegisterAssemblyTypes(assemblies.ToArray()).AsImplementedInterfaces().InstancePerLifetimeScope();
    18 
    19 
    20             //要注入的服务
    21             //builder.RegisterType<ServiceProvider>().As<IServiceProvider>();
    22         }

    2.startup配置

     public IServiceProvider ConfigureServices(IServiceCollection services)
            {
                //Autofac注入
                var containerBuilder = new ContainerBuilder();
                containerBuilder.RegisterModule<DefaulModule>();
                containerBuilder.Populate(services);
                var container = containerBuilder.Build();
                ObjectContainer.Current = container;
                return new AutofacServiceProvider(container);
            }
  • 相关阅读:
    nodejs安装以及配置
    java第三周学习总结
    java第二周学习总结
    java第一周学习总结
    调查问卷
    2016.2.19 学习总结
    第一周学习总结
    第一周学习总结
    假期马原学习计划
    20145335郝昊《java程序设计》第2次实验报告
  • 原文地址:https://www.cnblogs.com/lostsea/p/11573187.html
Copyright © 2020-2023  润新知