I got a SaaS project that needs the use Hangfire. We already implemented the requirements to identify a tenant.
Architecture
- Persistence Layer
- Each tenant has it's own database
- .NET Core
- We already have a service
TenantCurrentServicewhich returns the ID of the tenant, from a list of source [hostname, query string, etc] - We already have a
DbContextFactoryfor Entity Framework which return a DB context with the correct connection string for the client - We are currently using ASP.NET Core DI (willing to change if that helps)
- We already have a service
- Hangfire
- Using single storage (eg: Postgresql), no matter the tenant count
- Execute the job in an appropriate Container/ServiceCollection, so we retrieve the right database, right settings, etc.
The problem
I'm trying to stamp a TenantId to a job, retrieved from TenantCurrentService (which is a Scoped service).
When the job then gets executed, we need to retrieve the TenantId from the Job and store it in HangfireContext, so then the TenantCurrentService knows the TenantId retrieved from Hangfire. And from there, our application layer will be able to connect to the right database from our DbContextFactory
Current state
- Currently, we have been able to store tenantId retrieved from our Service using a
IClientFilter. - How can I retrieve my current ASP.NET Core DI ServiceScope from IServerFilter (which is responsible to retrieve the saved Job Parameters), so I can call .GetRequiredService().IdentifyTenant(tenantId)
Is there any good article regarding this matter / or any tips that you guys can provide?