ASP.NET Core support for multitenant DI via Autofac.Multitenant.
Please file issues and pull requests for this package in this repository rather than in the Autofac core repo.
ASP.NET Core default RequestServicesContainerMiddleware
is where the per-request lifetime scope usually gets generated. However, its constructor is where it wants the IServiceScopeFactory
that will be used later during the request to create the request lifetime scope.
Unfortunately, that means the IServiceScopeFactory
is created/resolved at the point when the request comes in, long before an HttpContext
is set in any IHttpContextAccessor
. The result is the scope factory ends up coming from the default tenant scope, before a tenant can be identified, and per-request services will later all come from the default tenant. Multitenancy fails.
This package provides a different request services middleware that ensures the IHttpContextAccessor.HttpContext
is set and defers creation of the request lifetime scope until as late as possible so anything needed for tenant identification can be established.