Finbuckle.MultiTenant icon indicating copy to clipboard operation
Finbuckle.MultiTenant copied to clipboard

Guidance for using library in background tasks and services

Open jeremysimmons opened this issue 3 years ago • 1 comments

What's the recommended approach for consuming the library in IHostedService / BackgroundService? The user will be submitting a job to a queue, including their tenant identifier. I wish to retrieve the custom ITenantInfo from the database, as it holds connection string and web api service urls. Seems like it's as simple as using EFCoreStoreDbContext<MyCustomTenantInfo> and a simple query to retrieve. _ctx.TenantInfo.FirstOrDefaultAsync(x => x.Identifier == identifier) Am I missing out on anything going this route?

jeremysimmons avatar Sep 09 '22 04:09 jeremysimmons

@jeremysimmons in this case I recommend emulating what the middleware does. Assuming you are using DI with the hosted service and configure the library normally you can use this logic in the appropriate place to resolve your tenant. You'll probably need a custom multi tenant strategy or use the delegate strategy:

var resolver = context.RequestServices.GetRequiredService<ITenantResolver>();
var multiTenantContext = await resolver.ResolveAsync(context);

AndrewTriesToCode avatar Sep 17 '22 02:09 AndrewTriesToCode

This issue has been automatically marked for closure because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 02 '22 06:11 stale[bot]