0

I set connection string in startup.cs but I need to change dbContext and hangfire connectionString according to httpContext request. I have some idea about dbContext but I don't have any idea about hangfire connString. How can I do this? Thanks.

My code in startup.cs like this:

var connStr = Configuration.GetConnectionString("DefaultConnection");

services.AddDbContext<ExampleContext>(options => options.UseSqlServer(connStr));
services.AddHangfire(x => x.UseSqlServerStorage(connStr));
services.AddHangfireServer();

I want to change "DefaultConnection" according to "HttpContext.Request.Host.Host".

fatih
  • 149
  • 1
  • 1
  • 12
  • Why would you even have multiple Hangfire databases though? The problem here is all this config is done at app startup, not during a request. – DavidG Jun 21 '23 at 09:18
  • However, this might be helpful https://stackoverflow.com/questions/57394712/hangfire-multi-tenant-asp-net-core-resolving-the-correct-tenant – DavidG Jun 21 '23 at 09:18
  • I will use a common api file and I will run this api on different IIS sites. Each site owner has own database. I cannot know which connection string belong to whom before first request. If I can do config in Configure method, it can be helpfull but I don't know how can I do there config settings for hangfire. – fatih Jun 21 '23 at 09:47
  • See https://docs.hangfire.io/en/latest/configuration/index.html – jdweng Jun 21 '23 at 09:54

0 Answers0