Hi,
Maybe this question has been answered, if so I would appreciate a link to it.
I want to load the tenants from a Db & create their respective databases (Each tenant to have their own database). I've tried overriding my DbContext onConfiguring but tenant is always null. See below code,
` public class CounterServeDefaultDbContext : DbContext
{
private readonly AppTenant tenant;
public CounterServeDefaultDbContext(AppTenant tenant)
{
this.tenant = tenant;
Database.EnsureCreated();
}
#region DbSets
public DbSet<AppTenantTuple> AppTenants { get; set; }
#endregion
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseNpgsql(tenant.ConnectionString);
base.OnConfiguring(optionsBuilder);
}
} `
StartUp Class
` public void ConfigureServices(IServiceCollection services)
{
services.AddMultitenancy<AppTenant, AppTenantResolver>();
services.AddMvcCore()
.AddAuthorization()
.AddJsonFormatters()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// AddAuthentication adds the authentication services to DI and configures "Bearer" as the default scheme
services.AddAuthentication("Bearer")
// AddIdentityServerAuthentication adds the IdentityServer access token validation handler into DI for use by the authentication services
.AddIdentityServerAuthentication(options =>
{
options.Authority = AUTHORITY;
options.RequireHttpsMetadata = false;
options.ApiName = "***";
});
services.AddEntityFrameworkNpgsql()
.AddDbContext<CounterServeDefaultDbContext>();
services.AddOptions();
services.AddSingleton<IConfigureOptions<MultitenancyOptions>, ConfigureMultitenancyOptions>();
// services.Configure<MultitenancyOptions>(Configuration.GetSection("Multitenancy"));
}`
Hi, I've just started using SaasKit.Multitenancy library. I'm looking for a way for Per-Tenant Options. Somethink like that in Finbuckle.MultiTenant:
services.AddMultiTenant()...
.WithPerTenantOptionsConfig<CookieAuthenticationOptions>((o, tenantContext) =>
{
o.Cookie.Name += tenantContext.Id;
});
How can you do it with SaasKit.Multitenancy?
InvalidOperationException: No service for type 'Models.Tenant' has been registered.
.Good morning guys!
I need to implement the filters in my services to get the data of the tenance you are requesting.
Given this scenario, will I have to inject the tenance resolver into my service and do a where in all methods manually, or is there any native saaskit implementation that solves this?
Example Today:
var accounts = _accountAppService.GetAsync (). Where (x => x.tenantId == _tenantResolver.Id)
and can't find any working example for core 2.0
this was the old code for core 1.0
var routeBuilder = new RouteBuilder(app);
var routeTemplate = "tenant/{*filePath}";
routeBuilder.MapRoute(routeTemplate, (IApplicationBuilder fork) =>
{
//Add middleware to rewrite our path for tenant specific files
fork.UseMiddleware<TenantSpecificPathRewriteMiddleware>();
fork.UseStaticFiles();
});
var router = routeBuilder.Build();
app.UseRouter(router);
but core 2.0 no longer use this. is there anyone with example on how to get this to work
@jeremycook Yes, it works in 3.0 but you can't override the default dependencies per tenant (https://benfoster.io/blog/asp-net-core-dependency-injection-multi-tenant). In ASP.NET Core 3.0, the way you override ConfigureServices is different. The SaasKit.Multitenancy.StructureMap extension doesn't work in 3.0 anymore. Seems like Microsoft removed the feature (aspnet/AspNetCore#5949).
In 2.2, you can override the ConfigureServices and configure the dependencies per tenant as shown in the code below:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
var container = new Container();
container.Populate(services);
container.ConfigureTenants<AppTenant>((tenant, config) =>
{
if (tenant.Db == 'MSSQL')
config.For<IRepository>().Use<MsSqlRepository>();
else
config.For<IRepository>().Use<MySqlRepositor>();
});
return container.GetInstance<IServiceProvider>();
}
services.AddScoped<IRepository>(serviceProvider => {
var tenant = serviceProvider.GetRequiredService<AppTenant>();
if (tenant.Db == "MSSQL")
return serviceProvider.GetRequiredService<MsSqlRepository>();
else
return serviceProvider.GetRequiredService<MySqlRepository>();
});