protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); //启用压缩 BundleTable.EnableOptimizations = true; BundleConfig.RegisterBundles(BundleTable.Bundles); //注入 Ioc var iocBuilder = new AutofacBuilder(); iocBuilder.RegisterDependencyResolver(builder => { builder.RegisterControllers(typeof(MvcApplication).Assembly); builder.Register(context => new _21EducationDbContext("name=_21Education")).AsSelf().SingleInstance(); //注册服务 var serviceType = typeof(IDependency); builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()) .Where(t => serviceType.IsAssignableFrom(t) && t != serviceType) .AsImplementedInterfaces().InstancePerLifetimeScope(); //注册实体 var entityType = typeof(IEntity); builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies()) .Where(t => entityType.IsAssignableFrom(t) && t != typeof(IDependency)) .AsSelf(); }); DependencyResolver.SetResolver(iocBuilder.Build()); }
public IServiceProvider ConfigureServices(IServiceCollection services) { this.config = new BoardlessWebConfig(this.configurationRoot); services.AddWebApiService(this.config); services.AddMvc().AddControllersAsServices(); var builder = new AutofacBuilder(); builder.Populate(services); builder.RegisterModule(new BoardlessWebDIModule(this.executeAssembly, this.rootPath, this.configurationRoot, this.loggerFactory)); var scope = builder.Build(); var migrationService = scope.Resolve <IMigrationUseCase>(); migrationService.ExecuteAsync().GetAwaiter().GetResult(); return(builder.CreateServiceProvider()); }
/// <summary> /// 実行 /// </summary> public int Execute() { var builder = new AutofacBuilder(); builder.RegisterModule(new SubscriberDIModule(this.executeAssembly, this.rootPath, this.configurationRoot, this.loggerFactory)); using (var scope = builder.Build()) { var cts = scope.Resolve <CancellationTokenSource>(); var action = new Action <AssemblyLoadContext>(context => { cts.Cancel(false); }); AssemblyLoadContext.Default.Unloading += action; try { var app = scope.Resolve <SubscriberApplication>(); return(app.Execute(args)); } finally { AssemblyLoadContext.Default.Unloading -= action; } } }
public void Build() { Container = AutofacBuilder.Build(); _hasActivationHooks = _activationHooks.Any(); }