I am trying to implement multi-tenancy in my Core 3.1 MVC app. I see examples such as below but when I run my app the HttpContext is null and I do not understand why. The only difference is I use ApplicationDbContext but from what I understand that it inherits from DbContext anyways...
public class PlaylistContext : DbContext
{
private int _tenantId;
private string _tenantHost;
public DbSet<Playlist> Playlists { get; set; }
public DbSet<Song> Songs { get; set; }
public PlaylistContext(DbContextOptions<PlaylistContext> options,
IHttpContextAccessor accessor)
: base(options)
{
_tenantHost = accessor.HttpContext.Request.Host.Value;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var connection = Database.GetDbConnection();
using (var command = connection.CreateCommand())
{
connection.Open();
command.CommandText = "select ID from Tenants where Host=@Host";
command.CommandType = CommandType.Text;
var param = command.CreateParameter();
param.ParameterName = "@Host";
param.Value = _tenantHost;
command.Parameters.Add(param);
_tenantId = (int)command.ExecuteScalar();
connection.Close();
}
foreach (var type in GetEntityTypes())
{
var method = SetGlobalQueryMethod.MakeGenericMethod(type);
method.Invoke(this, new object[] { modelBuilder });
}
base.OnModelCreating(modelBuilder);
}
// Other methods follow
}
My Code
public ApplicationDbContext(
DbContextOptions<ApplicationDbContext> options,
IHttpContextAccessor http,
ILogger<ApplicationDbContext> logger,
ITenantService tenantService
)
: base(options)
{
_http = http; // <-- _http.HttpContext is null
_logger = logger;
_tenantService = tenantService;
}
My Services
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection"));
options.EnableSensitiveDataLogging(true);
});
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddControllersWithViews()
.AddRazorRuntimeCompilation()
.AddNewtonsoftJson(options=>{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddRazorPages();
services.AddMultiTenancy()
.WithResolutionStrategy<HostResolutionStrategy>()
.WithStore<TenantStore>();
services.AddScoped<ITenantService, TenantService>();
}