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