Finbuckle implementation on Worker Service (.Net Core 3.1)
See original GitHub issueCan I just start by saying I absolutely love this library, I use it for one of my APIs and it works a treat.
I am now tying to configure a worker service as it’s own project and I want it to connect to different SQL Server databases using Finbuckle and EF Core. I have configured everything to the best of what I can, but I note that as I don’t have a Startup file and there is no middleware as I am not dealing with a request pipeline. Where do I need to add app.UseMultiTenant()? Alternatively is there a way I can pass a TenantInfo I obtained from a Configuration Store / Worker activity Directly to a dbcontext as part of a worker task?
Here is an extract from Program.cs code for the worker service
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging((context, logging) => {
logging.AddSerilog();
})
.ConfigureAppConfiguration((hostContext, config) =>
{
code to obtain configurations
})
.ConfigureServices((hostContext, services) =>
{
services.AddMultiTenant()
.WithStaticStrategy("dev")
.WithConfigurationStore(hostContext.Configuration, "FinbuckleSection");
services.AddDbContext<AppContext>();
services.AddHostedService<Worker>();
});
This is currently throwing a null reference exception for Connection String in the EF Core context which is inheriting from MultiTenantDbContext , and overriding OnConfiguring. This makes sense to me as at no point have I been able to pass the TenantInfo for a Worker Service task.
Thanks
H.
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (4 by maintainers)
hi @hhuseyinpay
I think this will work. I think it will run nonstop though – you might want to put it on a timer as described here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio#timed-background-tasks
Also you might want to include the look that sets up the
tenantRepo
list inside the timedwhile
look so it picks up any updates to the tenant in the tenant store.I’m not sure exactly if the lessions and servers items work as you intend since that’s specific to your app, but overall look good!
My final solution is:
It seems working for now but I don’t know what happens for the long-running. Any suggestion?