private static void ValidateFormStrategyConfiguration(FormStrategyConfiguration formStrategyConfiguration) { if (formStrategyConfiguration == null || formStrategyConfiguration.Parameters == null || formStrategyConfiguration.Parameters.Count == 0) { throw new MultiTenantException($"The configuration section does not contain any valid settings for the {nameof(FormStrategyConfiguration)}."); } }
public void FormStrategyConfiguration_Should_Bind(string section) { var configuration = SharedMock.GetConfigurationBuilder(SharedMock.NormalConfig).Build(); var c2 = new FormStrategyConfiguration(); configuration.GetSection(section).Bind(c2); Assert.NotNull(c2); Assert.Equal(2, c2.Parameters.Count); }
public FormStrategy(ILogger <FormStrategy> logger, IOptionsSnapshot <FormStrategyConfiguration> config) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _configuration = config?.Value ?? throw new ArgumentNullException(nameof(config)); if (!_configuration.Parameters?.Any() ?? false) { throw new MultiTenantException($"No values were provided for the {nameof(FormStrategyConfiguration)}."); } }
private static IWebHostBuilder GetTestHostBuilder(string routePattern, bool injectConfiguration) { return(new WebHostBuilder() .ConfigureAppConfiguration((hostContext, configApp) => { configApp.AddInMemoryCollection(SharedMock.NormalConfig); }) .ConfigureServices((ctx, services) => { var logger = new Mock <ILogger <FormStrategy> >(); services.AddScoped(sp => logger.Object); if (injectConfiguration) { services.AddMultiTenant() .WithFormStrategy(ctx.Configuration.GetSection("TenantConfiguration:FormStrategyConfiguration")) .WithInMemoryStore(); } else { var c2 = new FormStrategyConfiguration(); ctx.Configuration.GetSection("TenantConfiguration:FormStrategyConfiguration").Bind(c2); services.AddMultiTenant() .WithFormStrategy(c2) .WithInMemoryStore(); } services.AddMvc(); }) .Configure(app => { app.UseRouting(); app.UseMultiTenant(); app.UseEndpoints(endpoints => { endpoints.Map(routePattern, async context => { if (context.GetMultiTenantContext().TenantInfo != null) { await context.Response.WriteAsync(context.GetMultiTenantContext().TenantInfo.Id); } }); }); var store = app.ApplicationServices.GetRequiredService <IMultiTenantStore>(); PopulateTestStore(store); })); }
/// <summary> /// Adds and configures a FormStrategy to the application with a configuration section configuration which reloads with changes. /// </summary> /// <remarks> /// The form strategy does not rely on TenantContext. Therefore, it is not necessary to /// register the tenant context here. The only thing it relies on is a FormStrategyConfiguration /// object. This object is either added as a parameter or injected via IOptionsSnapshot. /// </remarks> /// <param name="builder"></param> /// <param name="formStrategyConfiguration">The configuration section defining the form strategy configuration and resolved with IOptionsSnapshot<></param> /// <returns></returns> public static FinbuckleMultiTenantBuilder WithFormStrategy(this FinbuckleMultiTenantBuilder builder, IConfigurationSection configurationSection) { // validate the configuration section var formStrategyConfiguration = new FormStrategyConfiguration(); configurationSection.Bind(formStrategyConfiguration); ValidateFormStrategyConfiguration(formStrategyConfiguration); // register configuration so it will reload with changes builder.Services.Configure <FormStrategyConfiguration>(configurationSection); // register the strategy with the built in finbuckly custom strategy builder.WithStrategy <FormStrategy>(ServiceLifetime.Scoped); return(builder); }
/// <summary> /// Adds and configures a FormStrategy to the application with a singleton configuration. /// </summary> /// <remarks> /// The form strategy does not rely on TenantContext. Therefore, it is not necessary to /// register the tenant context here. The only thing it relies on is a FormStrategyConfiguration /// object. This object is either added as a parameter or injected via IOptionsSnapshot. /// </remarks> /// <param name="builder"></param> /// <param name="formStrategyConfiguration">A static form strategy configuration</param> /// <returns></returns> public static FinbuckleMultiTenantBuilder WithFormStrategy(this FinbuckleMultiTenantBuilder builder, FormStrategyConfiguration formStrategyConfiguration) { // validate the configuration section ValidateFormStrategyConfiguration(formStrategyConfiguration); // wrap the configuration value in a IOptionsSnapshot object and register it builder.Services.AddSingleton <IOptionsSnapshot <FormStrategyConfiguration> >(new OptionSnapshotFormStrategyConfiguration(formStrategyConfiguration)); // register the strategy with the built in finbuckly custom strategy builder.WithStrategy <FormStrategy>(ServiceLifetime.Scoped); return(builder); }