I am using saaskit multi tenancy project for multi tenant application.
I am able to create the tenant context and able to access in all controllers.
However, sometimes it give me following error
An unhandled exception has occurred: Unable to resolve service for type 'LMS.Model.AppTenant' while attempting to activate 'LMS.Controllers.OrganizationController'.
This error occurs randomly for any controller and on any action method executed. The nature of error is not specific to any controller/action method.
I can see my code enters the TenantResolver successfully which does in fact return a Tenant but when it comes for DI to inject it into my controller it complains with the error listed
After this error occurs my application stop completely and it shows white screen.
This issue occurs on production server and not on localhost.
Any help on this !
Tenant injected in all controller like this
public class HomeController
{
public HomeController(TenantContext<AppTenant> tenantContext)
{
}
}
and below is the extension class for where tenantContext is made injectable
public static class MultitenancyServiceCollectionExtensions
{
public static IServiceCollection AddMultitenancy<TTenant, TResolver>(this IServiceCollection services)
where TResolver : class, ITenantResolver<TTenant>
where TTenant : class
{
Ensure.Argument.NotNull(services, nameof(services));
services.AddScoped<ITenantResolver<TTenant>, TResolver>();
// Make Tenant and TenantContext injectable
services.AddScoped(prov =>
prov.GetService<IHttpContextAccessor>()?.HttpContext?.GetTenant<TTenant>());
services.AddScoped(prov =>
prov.GetService<IHttpContextAccessor>()?.HttpContext?.GetTenantContext<TTenant>());
// Ensure caching is available for caching resolvers
services.AddMemoryCache();
return services;
}
}
Any help on this appreciated !