/// <summary>
    /// Configures startup modules with the specified configuration for <see cref="StartupModulesOptions"/>.
    /// </summary>
    /// <param name="services">The service collection to add the StartupModules services to.</param>
    /// <param name="configuration">The application's configuration.</param>
    /// <param name="environment">The application's environment information.</param>
    /// <param name="configure">A callback to configure <see cref="StartupModulesOptions"/>.</param>
    public static void AddStartupModules(this IServiceCollection services, IConfiguration configuration, IWebHostEnvironment environment, Action <StartupModulesOptions> configure)
    {
        if (services == null)
        {
            throw new ArgumentNullException(nameof(services));
        }
        if (configuration == null)
        {
            throw new ArgumentNullException(nameof(configuration));
        }
        if (environment == null)
        {
            throw new ArgumentNullException(nameof(environment));
        }

        var options = new StartupModulesOptions();

        configure(options);

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

        var runner = new StartupModuleRunner(options);

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

        var configureServicesContext = new ConfigureServicesContext(configuration, environment, options);

        runner.ConfigureServices(services, configuration, environment);
    }
Esempio n. 2
0
    public async Task RunsApplicationInitializers()
    {
        // Arrange
        var options = new StartupModulesOptions();

        options.ApplicationInitializers.Add(typeof(MyAppInitializer));
        var runner = new StartupModuleRunner(options);

        // Act
        await runner.RunApplicationInitializers(new ServiceCollection().BuildServiceProvider());

        // Assert
        // wat do ¯\_(ツ)_/¯
    }
Esempio n. 3
0
    public void Configures()
    {
        // Arrange
        var options       = new StartupModulesOptions();
        var startupModule = new MyStartupModule();

        options.AddStartupModule(startupModule);
        var runner = new StartupModuleRunner(options);

        // Act
        runner.Configure(new ApplicationBuilder(new ServiceCollection().BuildServiceProvider()), null, null);

        // Assert
        Assert.True(startupModule.Configured);
    }
Esempio n. 4
0
    public void ConfiguresServices()
    {
        // Arrange
        var options = new StartupModulesOptions();

        options.AddStartupModule <MyStartupModule>();
        var runner   = new StartupModuleRunner(options);
        var services = new ServiceCollection();

        // Act
        runner.ConfigureServices(services, null, null);

        // Assert
        var sd = Assert.Single(services);

        Assert.Equal(typeof(MyStartupModule.MyService), sd.ImplementationType);
    }
 /// <summary>
 /// Initializes a new instance of the <see cref="ModulesStartupFilter"/> class.
 /// </summary>
 public ModulesStartupFilter(StartupModuleRunner runner, IConfiguration configuration, IWebHostEnvironment hostingEnvironment)
 {
     _runner             = runner;
     _configuration      = configuration;
     _hostingEnvironment = hostingEnvironment;
 }