How to inject IHttpContextAccessor into Autofac TenantIdentificationStrategy

天涯浪子 提交于 2019-12-04 05:14:09

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>();.

This became to long for a comment.

First off your class is named SampleIdentificationStrategy but your constructor references AssetClassIdentificationStrategy. From this issue the project shouldnt even compile.

Next (as you havent provided a startup file) make sure you populating the registered services in AutoFac by calling the below code in the ConfigureServices method.

builder.Populate(services);
builder.Update(container);

Note this method must be run after you have registered all your services to the IServiceCollection

Next make sure you are not mixing framework versions. There are big differences between RC2 RC1, beta-x etc. This was noted here and here on the GitHub issue logs.

Other than this we need to see your startup.cs file (notably extracts from the ConfigureServices method, your project.json file (notably the frameworks and dependency nodes).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!