Skip to content

Commit

Permalink
Add Startup module settings
Browse files Browse the repository at this point in the history
  • Loading branch information
henkmollema committed Feb 21, 2020
1 parent 513b063 commit 8022314
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/StartupModules/ConfigureMiddlewareContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public class ConfigureMiddlewareContext
/// <summary>
/// Initializes a new instance of the <see cref="ConfigureServicesContext"/> class.
/// </summary>
public ConfigureMiddlewareContext(IConfiguration configuration, IWebHostEnvironment hostingEnvironment, IServiceProvider serviceProvider)
public ConfigureMiddlewareContext(IConfiguration configuration, IWebHostEnvironment hostingEnvironment, IServiceProvider serviceProvider, StartupModulesOptions options)
{
Configuration = configuration;
HostingEnvironment = hostingEnvironment;
ServiceProvider = serviceProvider;
Options = options;
}

/// <summary>
Expand All @@ -33,5 +34,10 @@ public ConfigureMiddlewareContext(IConfiguration configuration, IWebHostEnvironm
/// Gets the <see cref="IServiceProvider"/> instance scoped for the lifetime of application startup.
/// </summary>
public IServiceProvider ServiceProvider { get; }

/// <summary>
/// Gets the <see cref="StartupModulesOptions"/>.
/// </summary>
public StartupModulesOptions Options { get; }
}
}
8 changes: 7 additions & 1 deletion src/StartupModules/ConfigureServicesContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ public class ConfigureServicesContext
/// <summary>
/// Initializes a new instance of the <see cref="ConfigureServicesContext"/> class.
/// </summary>
public ConfigureServicesContext(IConfiguration configuration, IWebHostEnvironment hostingEnvironment)
public ConfigureServicesContext(IConfiguration configuration, IWebHostEnvironment hostingEnvironment, StartupModulesOptions options)
{
Configuration = configuration;
HostingEnvironment = hostingEnvironment;
Options = options;
}

/// <summary>
Expand All @@ -26,5 +27,10 @@ public ConfigureServicesContext(IConfiguration configuration, IWebHostEnvironmen
/// Gets the application <see cref="IWebHostEnvironment"/> instance.
/// </summary>
public IWebHostEnvironment HostingEnvironment { get; }

/// <summary>
/// Gets the <see cref="StartupModulesOptions"/>.
/// </summary>
public StartupModulesOptions Options { get; }
}
}
4 changes: 2 additions & 2 deletions src/StartupModules/StartupModuleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public StartupModuleRunner(StartupModulesOptions options)
/// </summary>
public void ConfigureServices(IServiceCollection services, IConfiguration configuration, IWebHostEnvironment hostingEnvironment)
{
var ctx = new ConfigureServicesContext(configuration, hostingEnvironment);
var ctx = new ConfigureServicesContext(configuration, hostingEnvironment, _options);

foreach (var cfg in _options.StartupModules)
{
Expand All @@ -45,7 +45,7 @@ public void ConfigureServices(IServiceCollection services, IConfiguration config
public void Configure(IApplicationBuilder app, IConfiguration configuration, IWebHostEnvironment hostingEnvironment)
{
using var scope = app.ApplicationServices.CreateScope();
var ctx = new ConfigureMiddlewareContext(configuration, hostingEnvironment, scope.ServiceProvider);
var ctx = new ConfigureMiddlewareContext(configuration, hostingEnvironment, scope.ServiceProvider, _options);
foreach (var cfg in _options.StartupModules)
{
cfg.Configure(app, ctx);
Expand Down
5 changes: 5 additions & 0 deletions src/StartupModules/StartupModulesOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ public class StartupModulesOptions
/// </summary>
public ICollection<Type> ApplicationInitializers { get; } = new List<Type>();

/// <summary>
/// Gets the settings.
/// </summary>
public IDictionary<string, object> Settings { get; set; } = new Dictionary<string, object>();

/// <summary>
/// Discovers <see cref="IStartupModule"/> implementations from the entry assembly of the application.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/StartupModules/WebHostBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static IWebHostBuilder UseStartupModules(this IWebHostBuilder builder, Ac
{
services.AddSingleton<IStartupFilter>(sp => ActivatorUtilities.CreateInstance<ModulesStartupFilter>(sp, runner));

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

Expand Down
5 changes: 4 additions & 1 deletion test/TestWebApp/HangfireStartupModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ public void ConfigureServices(IServiceCollection services, ConfigureServicesCont
// Do something based on this
}

services.AddHangfireContrib(c => c.UseMemoryStorage());
if ((bool)context.Options.Settings["AddHangfire"] == true)
{
services.AddHangfireContrib(c => c.UseMemoryStorage());
}
}

public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context) { }
Expand Down
2 changes: 1 addition & 1 deletion test/TestWebApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static void Main(string[] args)

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartupModules()
.UseStartupModules(x => x.Settings["AddHangfire"] = true)
.UseStartup<Startup>();
}
}

0 comments on commit 8022314

Please sign in to comment.