Search code examples
c#autofacasp.net-core-3.0

How to correctly configure InstancePerTenant using Autofac?


I have been trying to setup my component instance per each tenant using InstancePerTenant. However, the InstancePerTenant somehow behave like a SingleInstance. Is there something wrong with my setup?

I have this ContainerBuilder extension which configure the multitenant related dependencies.

public static ContainerBuilder AddMultitenancy(this ContainerBuilder builder)
{
  builder.RegisterType<HttpContextAccessor>().As<IHttpContextAccessor>().SingleInstance();
  builder.RegisterType<TenantStore>().As<ITenantStore>().SingleInstance();
  builder.RegisterType<TenantResolverStrategy>().As<ITenantIdentificationStrategy>().SingleInstance();
  return builder
}

The tenant is identified by hostname:

public class TenantResolverStrategy : ITenantIdentificationStrategy
{
  private readonly IHttpContextAccessor httpContextAccessor;

  public TenantResolverStrategy(
    IHttpContextAccessor httpContextAccessor
  )
  {
    this.httpContextAccessor = httpContextAccessor;
  }

  public bool TryIdentifyTenant(out object tenantId)
  {
    // hostname is the tenantId
    tenantId = httpContextAccessor.HttpContext?.Request?.Host.Value;
    return (tenantId != null || tenantId == (object)"");
  }
}

TenantStore is just a class to resolve the tenant entity from database based on the tenantId (hostname)

public class TenantStore : ITenantStore
{
  private readonly ITenantIdentificationStrategy tenantIdentificationStrategy;
  private readonly MemoryCacheStore cacheStore;
  private readonly ITenantService tenantService;

  public TenantStore(
    ITenantIdentificationStrategy tenantIdentificationStrategy,
    MemoryCacheStore cacheStore,
    ITenantService tenantService
  )
  {
    this.tenantIdentificationStrategy = tenantIdentificationStrategy;
    this.cacheStore = cacheStore;
    this.tenantService = tenantService;
  }

  public async Task<TenantEntity> GetTenantAsync(object tenantId)
  {
    var hostName = (string)tenantId;
    var tenant = cacheStore.Get<TenantEntity>(CacheType.Tenant, hostName);

    if (tenant == null)
    {
      tenant = await tenantService.GetTenantByHostNameFromDatabaseAsync(hostName);
      cacheStore.Set(tenant, CacheType.Tenant, hostName);
    }

    return tenant ?? new TenantEntity();
  }
}

In Startup.cs, I am registering TenantSpecific with InstancePerTenant:

public void ConfigureContainer(ContainerBuilder builder)
{
  builder.AddMultitenancy();
  builder.RegisterType<TenantSpecific>().As<ITenantSpecific>().InstancePerTenant();
}

public static MultitenantContainer ConfigureMultitenantContainer(IContainer container
{
  var strategy = container.Resolve<ITenantIdentificationStrategy>();
  var multitenantContainer = new MultitenantContainer(strategy, container);
  // Nothing important here
  multitenantContainer.RegisterMultitenantSpecificStuff();
  return multitenantContainer;
}

TenantSpecific.cs and TenantSpecificController.cs:

public class TenantSpecific
{
  public Guid Id { get; set; }

  public TenantSpecific()
  {
    this.Id = Guid.NewGuid();
  }
}

public class TenantSpecificController : ApiController
{
  private readonly ITenantSpecific tenantSpecific;

  public TenantSpecificController(ITenantSpecific tenantSpecific)
  {
    this.tenantSpecific = tenantSpecific;
  }

  [HttpGet]
  public IActionResult Get()
  {
    return Ok(tenantSpecific.Id);
  }
}

In Program.cs

public static IHostBuilder CreateHostBuilder(string[] args)
{
  var host = Host.CreateDefaultBuilder(args)
    .UseServiceProviderFactory(new AutofacMultitenantServiceProviderFactory(Startup.ConfigureMultitenantContainer))
    .ConfigureWebHostDefaults(webHostBuilder =>
    {
      webHostBuilder
        .UseConfiguration(ConfigurationModule.GetConfiguration())
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>();
    });
  return host;
}

When I invoke http://tenant1.localhost/tenant-specific/ and http://tenant2.localhost/tenant-specific, the constructor of TenantSpecific is only called once like Singleton. The tenantSpecific.Id returns the same value. So I assume InstancePerTenant is not working here.

Is there something wrong with my setup?


Solution

  • As written in the documentation ASP.net core multitenant support

    You should add a call to AddAutofacMultitenantRequestServices() to add the required middleware to the root container which is required for multitenancy to work.

    public void ConfigureServices(IServiceCollection services)
    {
        // This will all go in the ROOT CONTAINER and is NOT TENANT SPECIFIC.
        services.AddMvc();
        services.AddControllers();
    
        // This adds the required middleware to the ROOT CONTAINER and 
        // is required for multitenancy to work.
        services.AddAutofacMultitenantRequestServices();
    }