Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Hangfire recurring job - tenant specific #10338


User avatar
0
SBJ created
  • What is your product version? - 8.7.0
  • What is your product type (Angular or MVC)? - MVC
  • What is product framework type (.net framework or .net core)? - .NET Core
  • Database & Deployment Architectures - Single Deployment - Multiple Database

Hello, We are trying to implement Hangfire to run recurring background job. We have setup the initial steps of enabling HangfireDashboardEnabled and using the Hangfire dashboard. The Hangfire always uses the Default connectionString that is set in Startup.cs files in *.Web.MVC and *.Web.Host.

We would ideally want the jobs to be executed in the respective tenant database and not in the host database (Default).

Could you please suggest on how the connectionString for the Hangfire server be resolved dynamically based on logged in tenant.


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @SBJ

    You just need to start a UnitOfWork and swithc to target tenant context, see https://aspnetboilerplate.com/Pages/Documents/Multi-Tenancy#switching-between-host-and-tenants. In that using block, you can operate on target tenant's database.

  • User Avatar
    0
    SBJ created

    Hello @ismcagdas,

    Thanks for your suggestion. We tried to incorporate UnitOfWork but no luck. For normal database transactions like fetching data, we are able to retrieve/access SetTenantId()'s dedicated database but for a hangfire job it still refers to the Default (Host) database.

    For instance while setting the Db Connection for Hangfire, the connectionString is set to "Default" which will be the Host database. We assume that is the reason why it uses the Default(Host) database for Hangfire always irrespective of UnitOfWork.

     if (WebConsts.HangfireDashboardEnabled)
                {
                    //Hangfire (Enable to use Hangfire instead of default job manager)
                    services.AddHangfire(config =>
                    {
                        config.UseSqlServerStorage(_appConfiguration.GetConnectionString("Default"));
                    });
                }
    

    We need to switch the DbContext to dedicated databses for our Hangfire jobs too instead of using Default(Host) database for all tenants. Is there a way where we could resolve the connectionString and switch to dedicated tenant's database for Hangfire jobs too?

  • User Avatar
    0
    musa.demir created

    Hi @SBJ

    Saing jobs to tenant database is not supported.