0

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>();

        }
Mark Hollas
  • 1,107
  • 1
  • 16
  • 44

2 Answers2

0

Try adding this line into services registration:

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

or

services.AddHttpContextAccessor();

This link shows ways for accessing HttpContext:

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-3.1

Martin Staufcik
  • 8,295
  • 4
  • 44
  • 63
0

Be aware that the OnModelCreating will be called once even for new context. Also this will be executed outside of HttpRequest so that is why HttpContext is null.

services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddRoles<IdentityRole>() .AddEntityFrameworkStores<ApplicationDbContext>();

Support Ukraine
  • 978
  • 6
  • 20