Autofac Multitenant 容器是否与 IIndex 一起使用?<Key, Value>

问题描述 投票:0回答:1
在我的 WPF 应用程序中,我使用多租户(2 个租户)。

为了简化问题,我将展示示例:

var builder = new ContainerBuilder(); builder.RegisterModule<Module1>(); var container = builder.Build(); var singletonClass = container.Resolve<SingletonClass>(); ITenantIdentificationStrategy strategy = new TenantIdentificationStrategy(singletonClass); MultitenantContainer multitenantContainer = new MultitenantContainer(strategy, container); multitenantContainer.ConfigureTenant("A", c => c.RegisterModule<Module2>()); multitenantContainer.ConfigureTenant("B", c => c.RegisterModule<Module3>()); Interface1 interface1 = multitenantContainer.Resolve<Interface1>(); interface1.foo();
Interface1 - 不依赖于租户(在Module1中注册),
TenantIdentificationStrategy - 由SingletonClass提供TenantId,
Module2 和 Module3 - 通过 key 向共享 Interface2 注册不同的类。

Interface1有这样的实现:

internal class Service1 : Interface1 { private readonly IIndex<string, Interface2> index; private readonly SingletonClass singletonClass; public Service1(IIndex<string, Interface2> index, SingletonClass singletonClass) { this.index = index; this.singletonClass = singletonClass; } public void foo() { singletonClass.Id = "A"; if (index.TryGetValue("key", out var b)) { } else { } } }
IIndex 不返回服务。我注意到它甚至没有进入 TenantIdStrategy。
我在互联网上没有找到任何有关多租户和 IIndex 的信息。
请帮忙:)

.net wpf dependency-injection autofac multi-tenant
1个回答
0
投票
如果没有编译器来测试这一点,我的直觉是问题就在这里:

public void foo() { singletonClass.Id = "A"; if (index.TryGetValue("key", out var b)) { } else { } }
问题描述表明租户 ID 来自您的单例类。

这里的问题是,您在

Service1 类已解决之后在策略中设置租户 ID。

租户 ID 需要在解析时可用。
IIndex 不是“延迟解析” - 它将使用租户 ID 策略并从租户范围
在注入到 
Service1 构造函数
时进行解析。
在调用

Resolve<T>

之前设置单例类,我敢打赌它会按预期工作。

    

© www.soinside.com 2019 - 2024. All rights reserved.