How to inject IHttpContextAccessor into Autofac TenantIdentificationStrategy
There's not currently a way to inject things into a tenant identification strategy because the strategy itself doesn't go through the DI pipeline.
IHttpContextAccessor
is usually just backed with HttpContextAccessor
which is a singleton anyway and acts by getting info from async/thread local context. You could just new-up your strategy with one of these directly when you're in startup:
var strat = new MyStrategy(new HttpContextAccessor());
Note that at the time the question was originally asked there was an issue with the way multitenancy interacted with the ASP.NET Core IServiceProvider
system, which is to say, it didn't.
Since then, we've released 4.0.0-rc3-309
for the Autofac.Extensions.DependencyInjection
package which remedies the issue.
The change is that you need to update ConfigureServices
to return new AutofacServiceProvider(mtc);
and no longer do return mtc.Resolve<IServiceProvider>();
.