【问题标题】:How to use IHttpContextAccessor in Autofac Multitenant?如何在 Autofac 多租户中使用 IHttpContextAccessor?
【发布时间】: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


    【解决方案1】:

    在挖掘了一些源代码后,我从一个可以解决问题的测试中找到了 sample

    var strategy = new FooTenantStrategy(container.Resolve<IHttpContextAccessor>(), container.Resolve<ILogger<SonicFoundryTenantStrategy>>());
    

    关键部分是从之前构建的容器中提取上下文。

    【讨论】:

      猜你喜欢
      • 2021-03-20
      • 1970-01-01
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      相关资源
      最近更新 更多