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); }