我正在尝试解析ITenantIdentificationStrategy
中的ConfigureMultitenantContainer
,但我具有An unhandled exception of type 'Autofac.Core.DependencyResolutionException' occurred in Autofac.dll
。
我已经在TenantResolverStrategy
中注册了ConfigureContainer
:
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterType<TenantResolverStrategy>().As<ITenantIdentificationStrategy>();
}
我想解析ITenantIdentificationStrategy
中的ConfigureMultitenantContainer
:
public static MultitenantContainer ConfigureMultitenantContainer(IContainer container)
{
var strategy = container.Resolve<ITenantIdentificationStrategy>();
var mtc = new MultitenantContainer(strategy, container);
// mtc.ConfigureTenant("a", cb => cb.RegisterType<TenantACustom>().As<ITenantCustom>());
return mtc;
}
但是它抛出An unhandled exception of type 'Autofac.Core.DependencyResolutionException' occurred in Autofac.dll
。
我的ITenantIdentificationStrategy
是这样实现的:
public class TenantResolverStrategy : ITenantIdentificationStrategy
{
public TenantResolverStrategy(
IHttpContextAccessor httpContextAccessor,
IMemoryCache memoryCache,
TenantEntity tenantEntity
)
{
this.httpContextAccessor = httpContextAccessor;
this.memoryCache = memoryCache;
this.tenantEntity = tenantEntity;
}
public bool TryIdentifyTenant(out object tenantId)
{
tenantId = null;
var context = httpContextAccessor.HttpContext;
var hostName = context?.Request?.Host.Value;
tenantEntity = GetTenant(hostName);
if (tenantEntity != null)
{
tenantId = tenantEntity.TenantCode;
}
return (tenantId != null || tenantId == (object)"");
}
}
并且我将ConfigureMultitenantContainer
注册到Program.cs
中,如下所示:
var host = Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacMultitenantServiceProviderFactory(Startup.ConfigureMultitenantContainer))
我也无法解析我在ConfigureContainer
中注册的其他依赖项。我的实现有什么问题吗?