Skip to content

Commit

Permalink
Fix #976 - Throw an exception if a lifetime scope is created with a d…
Browse files Browse the repository at this point in the history
…uplicate tag
  • Loading branch information
Jeroen Weelink committed Apr 4, 2019
1 parent b79b388 commit 309d45a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
18 changes: 12 additions & 6 deletions src/Autofac/Core/Lifetime/LifetimeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,17 @@ public ILifetimeScope BeginLifetimeScope(object tag)
{
CheckNotDisposed();

CheckTagIsUnique(tag);

var registry = new CopyOnWriteRegistry(ComponentRegistry, () => CreateScopeRestrictedRegistry(tag, NoConfiguration));
var scope = new LifetimeScope(registry, this, tag);
scope.Disposer.AddInstanceForDisposal(registry);
RaiseBeginning(scope);
return scope;
}

private void CheckTagIsUnique(object tag)
{
ISharingLifetimeScope parentScope = this;
while (parentScope != RootLifetimeScope)
{
Expand All @@ -136,12 +147,6 @@ public ILifetimeScope BeginLifetimeScope(object tag)

parentScope = parentScope.ParentLifetimeScope;
}

var registry = new CopyOnWriteRegistry(ComponentRegistry, () => CreateScopeRestrictedRegistry(tag, NoConfiguration));
var scope = new LifetimeScope(registry, this, tag);
scope.Disposer.AddInstanceForDisposal(registry);
RaiseBeginning(scope);
return scope;
}

[SuppressMessage("CA1030", "CA1030", Justification = "This method raises the event; it's not the event proper.")]
Expand Down Expand Up @@ -200,6 +205,7 @@ public ILifetimeScope BeginLifetimeScope(object tag, Action<ContainerBuilder> co
if (configurationAction == null) throw new ArgumentNullException(nameof(configurationAction));

CheckNotDisposed();
CheckTagIsUnique(tag);

var locals = CreateScopeRestrictedRegistry(tag, configurationAction);
var scope = new LifetimeScope(locals, this, tag);
Expand Down
3 changes: 2 additions & 1 deletion test/Autofac.Specification.Test/Lifetime/NestedScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public void BeginLifetimeScopeCannotBeCalledWithDuplicateTag()
var taggedScope = rootScope.BeginLifetimeScope(duplicateTagName);
var differentTaggedScope = taggedScope.BeginLifetimeScope("DEF");

var exception = Assert.Throws<InvalidOperationException>(() => differentTaggedScope.BeginLifetimeScope(duplicateTagName));
Assert.Throws<InvalidOperationException>(() => differentTaggedScope.BeginLifetimeScope(duplicateTagName));
Assert.Throws<InvalidOperationException>(() => differentTaggedScope.BeginLifetimeScope(duplicateTagName, builder => builder.RegisterType<object>()));
}
}
}

0 comments on commit 309d45a

Please sign in to comment.