Skip to content

Integrating Mellon.MultiTenant with Hangfire in ASP.NET Core #29

@ShahramNo

Description

@ShahramNo

Hi,
I'm trying to integrate Mellon.MultiTenant into my existing multi-tenant setup with Hangfire. Could you provide guidance or examples on how to properly configure Mellon.MultiTenant with Hangfire? Specifically, I'm looking for help on how to ensure that Hangfire uses the correct connection string for each tenant.
I have 2 connection strings, Web is my default connection string when a tenant is not available, and Tenant stores all connection strings for every tenant, and i store all tenant in database,

Thank you in advance for your assistance!

appsetting:

{ "ConnectionStrings": { "Web": "Server=xxxx", "Tenant": "Server=xxx" } }

TenantModel:

`
```
public class Tenant : BaseEntity
{
public Guid Id { get; set; }
public string Name { get; set; }
public string? Host { get; set; }
public string SubDomain { get; set; } = null!;
public string ConnectionString { get; set; }
}

`

ITenantService:

` ```
 public interface ITenantService
   {
    Task IdentifyTenant(HttpContext context);
    Task<IEnumerable<Entities.Tenant.Tenant>> GetAllTenantsAsync();
    IEnumerable<Entities.Tenant.Tenant> GetAllTenants();
    Guid? GetTenantId(HttpContext context);
    string? GetTenantIdString(HttpContext context);
    Task<Entities.Tenant.Tenant> CreateTenantAsync(Entities.Tenant.Tenant tenant);
    Task<Entities.Tenant.Tenant> GetTenantByIdAsync(Guid id);
    string GetTenantConnectionString(HttpContext context);
    Task<Entities.Tenant.Tenant> GetCurrentTenantAsync(HttpContext context);
    void SetTenant(Entities.Tenant.Tenant tenant);
    string GetCurrentTenantConnectionString();
    Guid GetCurrentTenantId();
    string GetCurrentTenantIdString();
    Entities.Tenant.Tenant GetCurrentTenant();
    Task<Entities.Tenant.Tenant> GetCurrentTenantAsyncCaching(HttpContext context);
    string GetConnectionStringByTenantId(Guid tenantId);
    string GetDatabaseProvider();
    }

TenantResolutionMiddleware:

app.UseMiddleware<TenantResolutionMiddleware>();

` public class TenantResolutionMiddleware
{
private readonly RequestDelegate _next;

public TenantResolutionMiddleware(RequestDelegate next)
{
    _next = next;
}

public async Task InvokeAsync(HttpContext context, ITenantService tenantService)
{
    try
    {
        await tenantService.IdentifyTenant(context);
        var tenant = await tenantService.GetCurrentTenantAsync(context);
        if (tenant != null)
        {
            context.Items["ConnectionString"] = tenant.ConnectionString;
            context.Items["TenantConnectionString"] = tenant.ConnectionString;
            context.Items["TenantId"] = tenant.Id;
        }
        else
        {
            var defaultConnectionString = context.RequestServices.GetRequiredService<IConfiguration> 
                    ().GetConnectionString("Web");
            context.Items["ConnectionString"] = defaultConnectionString;
            context.Items["TenantConnectionString"] = defaultConnectionString;
        }
    }
    catch (Exception ex)
    {
        context.Response.StatusCode = 500; // Internal Server Error
        await context.Response.WriteAsync("An error occurred while identifying the tenant.");
        return;
    }
    await _next(context);
}

}`

InitializeTenantDatabases Middleware:

app.InitializeTenantDatabase(); app.InitializeTenantDatabases();

`public static class TenantDatabaseInitializer
{
public static void InitializeTenantDatabases(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var tenantService = scope.ServiceProvider.GetRequiredService();

    var tenants = tenantService.GetAllTenants();

    foreach (var tenant in tenants)
    {
        using var tenantScope = app.Services.CreateScope();
        var dbContext = tenantScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

        dbContext.Database.SetConnectionString(tenant.ConnectionString);
        dbContext.Database.Migrate();

        var dataInitializers = tenantScope.ServiceProvider.GetServices<IDataInitializer>();
        foreach (var dataInitializer in dataInitializers)
            dataInitializer.InitializeData();
    }
}

public static IApplicationBuilder InitializeTenantDatabase(this IApplicationBuilder app)
{
    using var scope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope();
    var dbContext = scope.ServiceProvider.GetService<TenantDbContext>(); // Service locator
    dbContext.Database.Migrate();
    return app;
}

}
`
Tenant Services:

builder.Services.AddTenantServices(builder.Configuration); builder.Services.AddDynamicDbContexts(builder.Configuration);

`public static class ServiceCollectionExtensions
{
public static IServiceCollection AddTenantServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext(options =>
options.UseSqlServer(configuration.GetConnectionString("Tenant")));

    services.AddScoped<ITenantService, TenantService>();
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddMemoryCache();
    services.AddLogging();

    return services;
}

public static IServiceCollection AddDynamicDbContexts(this IServiceCollection services, IConfiguration configuration)
{
    services.AddHttpContextAccessor();
    services.AddDbContext<ApplicationDbContext>((serviceProvider, options) =>
    {
        var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
        var connectionString = httpContextAccessor.HttpContext?.Items["ConnectionString"] as string
                              ?? configuration.GetConnectionString("Web");
        options.UseSqlServer(connectionString);
    });

    return services;
}

}`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions