TenantJobFilter<TKey> class
Namespace: Tenantry.Pro.Hangfire.Filters · Package: Tenantry.Pro.Hangfire · API reference
Hangfire filter that propagates the current tenant context into background jobs.
At enqueue time (TenantJobFilter<TKey>.OnCreating), the active tenant ID is stored as a Hangfire job parameter. At execution time (TenantJobFilter<TKey>.OnPerforming), the tenant is looked up from the store and the scope is restored, so any code running inside the job sees the correct ITenantContext<TKey>.
No-tenant jobs: If no tenant is active when the job is enqueued, the parameter is omitted and the job executes without a tenant scope.
Sync-over-async: Hangfire server filters are synchronous. ITenantStore<TKey>.GetTenantAsync is called with .GetAwaiter().GetResult(), which blocks the Hangfire worker thread briefly. This is acceptable because Hangfire workers run on the thread pool — not on ASP.NET request threads — and tenant stores are typically backed by fast, cached lookups.
Register via pro.AddHangfireTenantFilter() and wire into Hangfire using app.UseTenantryHangfire<TKey>().
public sealed class TenantJobFilter<TKey> : IClientFilter, IServerFilter where TKey : IEquatable<TKey>, IParsable<TKey>Type parameters
TKey: The tenant identifier type.
Implements IClientFilter, IServerFilter.
Constructors
TenantJobFilter(ITenantContext<TKey>, ITenantScope<TKey>, ITenantStoreAccessor<TKey>, MissingTenantBehavior, ILogger<TenantJobFilter<TKey>>)
Hangfire filter that propagates the current tenant context into background jobs.
public TenantJobFilter(ITenantContext<TKey> tenantContext, ITenantScope<TKey> tenantScope, ITenantStoreAccessor<TKey> storeAccessor, MissingTenantBehavior onMissingTenant, ILogger<TenantJobFilter<TKey>> logger)Parameters:
tenantContextITenantContext<TKey>: Supplies the current tenant when a job is created.tenantScopeITenantScope<TKey>: Runs each job under the tenant it was created for.storeAccessorITenantStoreAccessor<TKey>: Looks up the job's tenant before it runs.onMissingTenantMissingTenantBehavior: What to do with a job that carries no tenant, or one the store does not know.loggerILogger<TenantJobFilter<TKey>>: Logs jobs that run without a tenant.
At enqueue time (TenantJobFilter<TKey>.OnCreating), the active tenant ID is stored as a Hangfire job parameter. At execution time (TenantJobFilter<TKey>.OnPerforming), the tenant is looked up from the store and the scope is restored, so any code running inside the job sees the correct ITenantContext<TKey>.
No-tenant jobs: If no tenant is active when the job is enqueued, the parameter is omitted and the job executes without a tenant scope.
Sync-over-async: Hangfire server filters are synchronous. ITenantStore<TKey>.GetTenantAsync is called with .GetAwaiter().GetResult(), which blocks the Hangfire worker thread briefly. This is acceptable because Hangfire workers run on the thread pool — not on ASP.NET request threads — and tenant stores are typically backed by fast, cached lookups.
Register via pro.AddHangfireTenantFilter() and wire into Hangfire using app.UseTenantryHangfire<TKey>().
Methods
OnCreated(CreatedContext)
Called after the creation of the job.
public void OnCreated(CreatedContext filterContext)Parameters:
filterContextCreatedContext: The filter context.
OnCreating(CreatingContext)
Called before the creation of the job.
public void OnCreating(CreatingContext filterContext)Parameters:
filterContextCreatingContext: The filter context.
OnPerformed(PerformedContext)
Called after the performance of the job.
public void OnPerformed(PerformedContext filterContext)Parameters:
filterContextPerformedContext: The filter context.
OnPerforming(PerformingContext)
Called before the performance of the job.
public void OnPerforming(PerformingContext filterContext)Parameters:
filterContextPerformingContext: The filter context.