Esempio n. 1
0
        /// <summary>
        /// Configures startup modules with the specified configuration for <see cref="StartupModulesOptions"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IWebHostBuilder"/> instance.</param>
        /// <param name="configure">A callback to configure <see cref="StartupModulesOptions"/>.</param>
        /// <returns>The <see cref="IWebHostBuilder"/> instance.</returns>
        public static IWebHostBuilder UseStartupModules(this IWebHostBuilder builder, Action <StartupModulesOptions> configure)
        {
            if (builder == null)
            {
                throw new ArgumentNullException(nameof(builder));
            }

            if (configure == null)
            {
                throw new ArgumentNullException(nameof(configure));
            }

            var options = new StartupModulesOptions();

            configure(options);

            if (options.StartupModules.Count == 0 && options.ApplicationInitializers.Count == 0)
            {
                // Nothing to do here
                return(builder);
            }

            var runner = new StartupModuleRunner(options);

            builder.ConfigureServices((hostContext, services) =>
            {
                services.AddSingleton <IStartupFilter>(sp => ActivatorUtilities.CreateInstance <ModulesStartupFilter>(sp, runner));

                var configureServicesContext = new ConfigureServicesContext(hostContext.Configuration, hostContext.HostingEnvironment, options);
                runner.ConfigureServices(services, hostContext.Configuration, hostContext.HostingEnvironment);
            });

            return(builder);
        }
Esempio n. 2
0
        /// <summary>
        /// Calls <see cref="IStartupModule.ConfigureServices(IServiceCollection, ConfigureServicesContext)"/> on the
        /// discoverd <see cref="IStartupModule"/>'s.
        /// </summary>
        public void ConfigureServices(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment hostingEnvironment)
        {
            var ctx = new ConfigureServicesContext(configuration, hostingEnvironment, _options);

            foreach (var cfg in _options.StartupModules)
            {
                cfg.ConfigureServices(services, ctx);
            }
        }