在ConfigureMultitenantContainer中解析依赖项

问题描述 投票:1回答:1

我正在尝试解析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中注册的其他依赖项。我的实现有什么问题吗?

c# dependency-injection autofac ioc-container asp.net-core-3.0
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.