Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/Autofac.Multitenant/MultitenantContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,25 +394,43 @@ private ILifetimeScope CreateTenantScope(object tenantId, Action<ContainerBuilde
/// <returns><c>true</c> if an existing configuration was removed; otherwise, <c>false</c>.</returns>
/// <seealso cref="ConfigurationActionBuilder"/>
/// <seealso cref="ConfigureTenant(object, Action{ContainerBuilder})"/>
[SuppressMessage("CA1513", "CA1513", Justification = "ObjectDisposedException.ThrowIf is not available in all target frameworks.")]
public bool ReconfigureTenant(object tenantId, Action<ContainerBuilder> configuration)
{
if (configuration == null)
{
throw new ArgumentNullException(nameof(configuration));
}

if (_isDisposed == 1)
{
throw new ObjectDisposedException(nameof(ApplicationContainer));
}

tenantId ??= _defaultTenantId;

var removed = false;
if (_tenantLifetimeScopes.TryRemove(tenantId, out var tenantScope))
var lifetimeScope = ApplicationContainer.BeginLifetimeScope(TenantLifetimeScopeTag, configuration);

ILifetimeScope? disposedLifetimeScope = null;
_tenantLifetimeScopes.AddOrUpdate(
tenantId,
_ => lifetimeScope,
(_, updatedLifetimeScope) =>
{
// We defer the existing scope Dispose()
// as we prefer enabling the new scope as quickly
// as possible.
disposedLifetimeScope = updatedLifetimeScope;
return lifetimeScope;
});

if (disposedLifetimeScope == null)
{
tenantScope.Dispose();
removed = true;
return false;
}

CreateTenantScope(tenantId, configuration);

return removed;
disposedLifetimeScope.Dispose();
return true;
}

/// <summary>
Expand Down
46 changes: 46 additions & 0 deletions test/Autofac.Multitenant.Test/MultitenantContainerFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,52 @@ public void ReconfigureTenant_ReconfigureSingleton()
Assert.IsType<StubDependency1Impl3>(mtc.Resolve<IStubDependency1>());
}

[Fact]
public void ReconfigureTenant_RequiresConfiguration()
{
var builder = new ContainerBuilder();
var strategy = new StubTenantIdentificationStrategy()
{
TenantId = "tenant1",
};
using var mtc = new MultitenantContainer(strategy, builder.Build());
mtc.ConfigureTenant("tenant1", b => b.RegisterType<StubDependency1Impl2>().AsImplementedInterfaces().SingleInstance());

Assert.Throws<ArgumentNullException>(() => mtc.ReconfigureTenant("tenant1", null));
}

[Fact]
public void ReconfigureTenant_ThrowsAfterDisposal()
{
var builder = new ContainerBuilder();
var strategy = new StubTenantIdentificationStrategy()
{
TenantId = "tenant1",
};
using var mtc = new MultitenantContainer(strategy, builder.Build());
mtc.Dispose();
Assert.Throws<ObjectDisposedException>(() => mtc.ReconfigureTenant("tenant1", _ => { }));
}

[Fact]
public void ReconfigureTenant_AddLifetimeScope()
{
var builder = new ContainerBuilder();
var strategy = new StubTenantIdentificationStrategy()
{
TenantId = "tenant1",
};
using var mtc = new MultitenantContainer(strategy, builder.Build());
mtc.ConfigureTenant("tenant1", b => b.RegisterType<StubDependency1Impl2>().AsImplementedInterfaces().SingleInstance());

// Simulate another thread playing with the MTC
mtc.RemoveTenant("tenant1");

mtc.ReconfigureTenant("tenant1", b => b.RegisterType<StubDependency1Impl3>().AsImplementedInterfaces().SingleInstance());

Assert.IsType<StubDependency1Impl3>(mtc.Resolve<IStubDependency1>());
}

[Fact]
public void GetTenants_CheckRegistered()
{
Expand Down