/// <summary> /// Initializes a new instance of the <see cref="ModulesStartupFilter"/> class. /// </summary> public ModulesStartupFilter(StartupModulesOptions options, IConfiguration configuration, IHostingEnvironment hostingEnvironment, ILogger <ModulesStartupFilter> logger) { _options = options; _configuration = configuration; _hostingEnvironment = hostingEnvironment; _logger = logger; }
/// <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); }
/// <summary> /// Initializes a new instance of the <see cref="ConfigureServicesContext"/> class. /// </summary> public ConfigureMiddlewareContext(IConfiguration configuration, IWebHostEnvironment hostingEnvironment, IServiceProvider serviceProvider, StartupModulesOptions options) { Configuration = configuration; HostingEnvironment = hostingEnvironment; ServiceProvider = serviceProvider; Options = options; }
public void ThrowsException_WithErrorConstructor() { var options = new StartupModulesOptions(); var ex = Assert.Throws <InvalidOperationException>(() => options.AddStartupModule <StartupModuleWithErrorCtor>()); var expectedMsg = $"Failed to create instance for {nameof(IStartupModule)} type '{typeof(StartupModuleWithErrorCtor).Name}'."; Assert.Equal(expectedMsg, ex.Message); }
public void ThrowsException_WhenTypeNotIStartupModule() { var options = new StartupModulesOptions(); var ex = Assert.Throws <ArgumentException>("type", () => options.AddStartupModule(typeof(decimal))); var expectedEx = new ArgumentException($"Specified startup module '{typeof(decimal).Name}' does not implement {nameof(IStartupModule)}.", "type"); Assert.Equal(expectedEx.Message, ex.Message); }
public void ThrowsException_WhenNoParameterlessConstructor() { var options = new StartupModulesOptions(); var ex = Assert.Throws <InvalidOperationException>(() => options.AddStartupModule <StartupModuleWithCtor>()); var expectedMsg = $"创建实例未 {nameof(IStartupModule)}的实例失败 name: '{typeof(StartupModuleWithCtor).Name}'."; Assert.Equal(expectedMsg, ex.Message); }
public void ThrowsException_WithErrorConstructor() { var options = new StartupModulesOptions(); var ex = Assert.Throws <InvalidOperationException>(() => options.AddStartupModule <StartupModuleWithErrorCtor>()); var expectedMsg = $"创建实例未 {nameof(IStartupModule)}的实例失败 name: '{typeof(StartupModuleWithErrorCtor).Name}'."; //$"Specified startup module '{type.Name}' does not implement {nameof(IStartupModule)}." Assert.Equal(expectedMsg, ex.Message); }
public void AddStartupModule_FromInstance() { var options = new StartupModulesOptions(); options.AddStartupModule(new FooStartupModule()); var module = Assert.Single(options.StartupModules); Assert.IsType <FooStartupModule>(module); }
public void AddStartupModule_FromGenericType() { var options = new StartupModulesOptions(); options.AddStartupModule <FooStartupModule>(); var module = Assert.Single(options.StartupModules); Assert.IsType <FooStartupModule>(module); }
public void ConfigureMiddleware_AddsStartupModule() { var options = new StartupModulesOptions(); options.ConfigureMiddleware((app, ctx) => { }); var module = Assert.Single(options.StartupModules); Assert.IsType <InlineMiddlewareConfiguration>(module); module.ConfigureServices(null, null); module.Configure(null, null); }
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 ¯\_(ツ)_/¯ }
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); }
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="ConfigureServicesContext"/> class. /// </summary> public ConfigureServicesContext(IConfiguration configuration, IWebHostEnvironment hostingEnvironment, StartupModulesOptions options) { Configuration = configuration; HostingEnvironment = hostingEnvironment; Options = options; }
/// <summary> /// Initializes a new instance of the <see cref="StartupModuleRunner"/> class. /// </summary> /// <param name="options">The <see cref="StartupModulesOptions"/> to discover <see cref="IStartupModule"/>'s.</param> public StartupModuleRunner(StartupModulesOptions options) { _options = options; }