Hi
I am struggling to get the hostname strategy working. I'm using the EFCore InMemory store, which I populate with "TenantInfo" items. I've assumed the hostname should be set against the "Identifier" property so my setup looks like the following:
public void ConfigureServices(IServiceCollection services)
{
// Register options
services.AddOptions();
services.AddMediatR(typeof(RetrieveTenants.Handler).Assembly);
#region Tenant Db Configuration
// Tenant database connection
services.AddDbContext<ConfiguratorDbContext>(options =>
options.UseSqlServer(Configuration["TenantDbConnection:ConnectionString"]));
#endregion
services.AddLogging();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddFluentValidation(cfg =>
{
cfg.RegisterValidatorsFromAssemblyContaining(typeof(AbstractValidator<>));
});
#region Multitenancy configuration
services.AddMultiTenant()
.WithEFCoreStore<StoreDbContext, AppTenantInfo>()
.WithHostStrategy();
#endregion
#region Application Db Configuration
// Register the db context, but do not specify a provider/connection string since
// these vary by tenant.
services.AddDbContext<WintrixDbContext>();
#endregion
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseHsts();
}
app.UseStaticFiles();
app.UseMultiTenant();
app.UseMvc();
SetupStore(app.ApplicationServices);
}
private void SetupStore(IServiceProvider sp)
{
var scopeServices = sp.CreateScope().ServiceProvider;
var store = scopeServices.GetRequiredService<IMultiTenantStore>();
var mediator = scopeServices.GetRequiredService<IMediator>();
var tenants = mediator
.Send(new RetrieveTenants.Query
{
Environment = Configuration["TenantDbConnection:Environment"]
})
.GetAwaiter()
.GetResult()
.Tenants;
foreach (var tenant in tenants)
{
store.TryAddAsync(new TenantInfo(tenant.Id, tenant.Hostname, tenant.Name, tenant.ConnectionString, null)).Wait();
}
}
Currently I seem to get no tenantInfo when I call:
HttpContext.GetMultiTenantContext()?.TenantInfo;
My setup is meant to use our tenant database to get the tenant configuration data including connection string and hostnames. I then load this in to a TenantInfo objects with the hostname as the Identifier and insert this in to an EFCore store.
That part seems to work ok becuase I see all the tenants if I query the StoreDbContext directly.
I've added bindings in IIS for each host name and added entries to my host file.
Each tenant has a separate database which should then be resolved by the tenant connection string in the EFCoreStore, but as I said above, in my controller the tenantInfo is currently null?
Any help would be much appreciated
I guess one thing I thought about, was in your hostname sample you only replaced the subdomain, in my case the whole host name is different.
i.e.
www.something.com
another.domain.net
Andy