Replies: 2 comments
-
Well I made slight progress... I attempted to use I am still stuck though. I cannot figure out why the injected property |
Beta Was this translation helpful? Give feedback.
-
If you want to set a static tenant for the whole application, you can just use a static strategy (https://www.finbuckle.com/MultiTenant/Docs/v6.12.0/Strategies#static-strategy) when configuring multitenancy at application startup. Setting it dynamically has its quircks, as the tenantInfo is retrieved from DI, and setting the tenantInfo using HttpContext.TrySetTenantInfo, will not update the tenantInfo that's for instance embedded inside the DbContext that could already been retrieved from DI before you called the TrySetTenantInfo. What I usually do is create a new serviceScope using IServiceScopeFactory.CreateScope and then set the new tenantInfo in that Scope, before executing whatever you want to do inside that new scope: using var scope = _services.CreateScope();
// Set current tenant
scope.ServiceProvider.GetRequiredService<IMultiTenantContextAccessor<FSHTenantInfo>>()
.MultiTenantContext = new MultiTenantContext<FSHTenantInfo>() { TenantInfo = tenant };
await scope.ServiceProvider.GetRequiredService<ISender>()
.Send(new SomethingRequest(parameters), ct); (it's also done this way in the DatabaseInitializer, to initialize the database for each tenant) |
Beta Was this translation helpful? Give feedback.
-
I understand the current tenant is set from the request header. However, my use case requires I set the tenant manually without needing to take in a header. How can this be done?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions