예제 #1
0
        public virtual void BootstrapHost(BootstrapModuleHostCommand <TModule> command)
        {
            if (command.Host != null)
            {
                return;
            }

            BootstrapContext = command.BootstrapContext;

            var builder = CreateHostBuilder();

            ApplyConfiguration(builder);
            builder.ConfigureServices((ctx, services) =>
            {
                // ReSharper disable once ConvertToUsingDeclaration
                using (var tempServiceProvider = services.BuildServiceProvider())
                {
                    ConfigureServices(ctx, services, tempServiceProvider);
                }
            });


            command.Options.ConfigureBuilderAction?.Invoke(builder);
            command.Host          = builder.Build();
            command.ModuleContext = CreateModuleContext(command.Host.Services);
        }
        public override void BootstrapHost(BootstrapModuleHostCommand <TModule> command)
        {
            if (command.Host != null || !(command.BootstrapContext.Module is IWebModule))
            {
                return;
            }

            BootstrapContext = command.BootstrapContext;

            var webHostBuilderFilters = BootstrapContext.Advanced.FrameworkServices
                                        .GetServices <IWebModuleWebHostBuilderFilter>();

            var moduleAssemblyName = BootstrapContext.Module.GetType().Assembly.GetName().Name;


#if NETCOREAPP
            var webHostBuilderInitializer = BootstrapContext.Advanced.FrameworkServices
                                            .GetService <IWebModuleWebHostBuilderInitializer>();

            if (webHostBuilderInitializer == null)
            {
                throw new InvalidOperationException("AspNetCore runtime not configured.");
            }


            var builder = CreateHostBuilder();

            webHostBuilderInitializer.ConfigureWebHost(BootstrapContext.Module as IWebModule, builder,
                                                       new[]
            {
                new DelegateWebModuleWebHostBuilderFilter((_, webHostBuilder) =>
                {
                    webHostBuilder.ConfigureAppConfiguration(
                        (ctx, b) =>
                    {
                        ctx.HostingEnvironment.ApplicationName = moduleAssemblyName;
                    });

                    webHostBuilder.ConfigureServices((ctx, services) =>
                    {
                        // ReSharper disable once ConvertToUsingDeclaration
                        using (var tempServiceProvider = services.BuildServiceProvider())
                        {
                            ConfigureServices(WebContextToHostBuilderContext(ctx), services, tempServiceProvider);
                        }
                    });
                })
            }.Union(webHostBuilderFilters)
                                                       .Append(
                                                           new DelegateWebModuleWebHostBuilderFilter((_, webHostBuilder) =>
            {
                AddModuleContent(webHostBuilder);

                webHostBuilder.Configure(app =>
                {
                    // ReSharper disable once AccessToModifiedClosure
                    Configure(command.ModuleContext, app);
                });
            })));
            ApplyConfiguration(builder);

            command.Options.ConfigureBuilderAction?.Invoke(builder);

            command.Host          = builder.Build();
            command.ModuleContext = CreateModuleContext(command.Host.Services);
#else
            if (command.Options.ConfigureBuilderAction != null)
            {
                throw new InvalidOperationException("The configure method with IHostBuilder is not supported for ASPNETCORE < 3.0. You should configure the webHostBuilder instead.");
            }

            var webHostBuilderFactory = BootstrapContext.Advanced.FrameworkServices
                                        .GetService <IWebModuleWebHostBuilderFactory>();

            if (webHostBuilderFactory == null)
            {
                throw new InvalidOperationException("AspNetCore runtime not configured.");
            }


            var webHostBuilder     = webHostBuilderFactory.CreateWebHost(BootstrapContext.Module as IWebModule);
            var hostBuilderContext = BootstrapContext.Advanced.FrameworkServices.GetRequiredService <HostBuilderContext>();
            webHostBuilder.UseConfiguration(hostBuilderContext.Configuration);

            if (hostBuilderContext.HostingEnvironment.IsDevelopment())
            {
                webHostBuilder.UseContentRoot(GetRelativeModulePath());
            }

            webHostBuilder.ConfigureAppConfiguration((ctx, config) =>
            {
                ctx.HostingEnvironment.ApplicationName = moduleAssemblyName;

                Filters.BuildFilterPipeline(
                    BootstrapContext.Advanced.FrameworkServices.GetServices <IModuleConfigurationFilter>(),
                    (_, __) => { })(BootstrapContext.ToModuleHostBuilderContext(WebContextToHostBuilderContext(ctx)), config);
            });

            Filters.BuildFilterPipeline(webHostBuilderFilters, (_, __) => { })(BootstrapContext.Module as IWebModule, webHostBuilder);


            webHostBuilder.ConfigureServices((webContext, services) =>
            {
                var tempServiceProvider = services.BuildServiceProvider();

                Action <IServiceCollection> configureMethod = (s) =>
                {
                    ConfigureServices(WebContextToHostBuilderContext(webContext), s, tempServiceProvider);
                };

                configureMethod = BuildConfigureServicesFilterPipeline(tempServiceProvider, configureMethod);
                configureMethod(services);
            });

            webHostBuilder.Configure(app =>
            {
                // ReSharper disable once AccessToModifiedClosure
                Configure(command.ModuleContext, app);
            });

            command.Options.ConfigureWebHostBuilder(webHostBuilder);
            var webHost = command.Options.BuildWebHost(webHostBuilder);

            command.ModuleContext = CreateModuleContext(webHost.Services);
            command.Host          = new WebHostWrapperHost(webHost);
#endif
        }