【发布时间】:2019-12-01 18:25:37
【问题描述】:
使用 Autofac 将 IHttpContextAccessor 传递到多租户策略的正确方法是什么?我似乎在任何地方都找不到这个记录。我尝试构建 HttpContextAccessor 的实例并将其传递给策略,但这会导致 HttpContext 始终为空。
启动
public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMvc();
var builder = new ContainerBuilder();
builder.Populate(services);
var container = builder.Build();
var strategy = new FooTenantStrategy(new HttpContextAccessor());
var mtc = new MultitenantContainer(strategy, container);
Startup.ApplicationContainer = mtc;
return new AutofacServiceProvider(mtc);
}
计划
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
// This enables the request lifetime scope to be properly spawned from
// the container rather than be a child of the default tenant scope.
// The ApplicationContainer static property is where the multitenant container
// will be stored once it's built.
.UseAutofacMultitenantRequestServices(() => Startup.ApplicationContainer)
.UseStartup<Startup>();
【问题讨论】:
标签: asp.net-core dependency-injection autofac multi-tenant