This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from Particular/hotfix-5.1.1
Externally owned container is no longer disposed
- Loading branch information
Showing
10 changed files
with
273 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
namespace NServiceBus.Autofac.AcceptanceTests | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using global::Autofac; | ||
using global::Autofac.Core; | ||
using global::Autofac.Core.Lifetime; | ||
using global::Autofac.Core.Resolving; | ||
|
||
class ScopeDecorator : ILifetimeScope | ||
{ | ||
public ScopeDecorator(ILifetimeScope decorated) | ||
{ | ||
this.decorated = decorated; | ||
} | ||
|
||
public bool Disposed { get; private set; } | ||
|
||
public object ResolveComponent(IComponentRegistration registration, IEnumerable<Parameter> parameters) | ||
{ | ||
return decorated.ResolveComponent(registration, parameters); | ||
} | ||
|
||
public IComponentRegistry ComponentRegistry => decorated.ComponentRegistry; | ||
|
||
public void Dispose() | ||
{ | ||
decorated.Dispose(); | ||
Disposed = true; | ||
} | ||
|
||
public ILifetimeScope BeginLifetimeScope() | ||
{ | ||
return decorated.BeginLifetimeScope(); | ||
} | ||
|
||
public ILifetimeScope BeginLifetimeScope(object tag) | ||
{ | ||
return decorated.BeginLifetimeScope(tag); | ||
} | ||
|
||
public ILifetimeScope BeginLifetimeScope(Action<ContainerBuilder> configurationAction) | ||
{ | ||
return decorated.BeginLifetimeScope(configurationAction); | ||
} | ||
|
||
public ILifetimeScope BeginLifetimeScope(object tag, Action<ContainerBuilder> configurationAction) | ||
{ | ||
return decorated.BeginLifetimeScope(tag, configurationAction); | ||
} | ||
|
||
public IDisposer Disposer => decorated.Disposer; | ||
|
||
public object Tag => decorated.Tag; | ||
|
||
public event EventHandler<LifetimeScopeBeginningEventArgs> ChildLifetimeScopeBeginning | ||
{ | ||
add { decorated.ChildLifetimeScopeBeginning += value; } | ||
remove { decorated.ChildLifetimeScopeBeginning -= value; } | ||
} | ||
|
||
public event EventHandler<LifetimeScopeEndingEventArgs> CurrentScopeEnding | ||
{ | ||
add { decorated.CurrentScopeEnding += value; } | ||
remove { decorated.CurrentScopeEnding -= value; } | ||
} | ||
public event EventHandler<ResolveOperationBeginningEventArgs> ResolveOperationBeginning | ||
{ | ||
add { decorated.ResolveOperationBeginning += value; } | ||
remove { decorated.ResolveOperationBeginning -= value; } | ||
} | ||
|
||
private readonly ILifetimeScope decorated; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/NServiceBus.Autofac.AcceptanceTests/When_using_externally_owned_container.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
namespace NServiceBus.AcceptanceTests | ||
{ | ||
using global::Autofac; | ||
using NServiceBus.AcceptanceTesting; | ||
using NServiceBus.AcceptanceTests.EndpointTemplates; | ||
using NServiceBus.Autofac.AcceptanceTests; | ||
using NUnit.Framework; | ||
|
||
public class When_using_externally_owned_container : NServiceBusAcceptanceTest | ||
{ | ||
[Test] | ||
public void Should_shutdown_properly() | ||
{ | ||
Scenario.Define<Context>() | ||
.WithEndpoint<Endpoint>() | ||
.Done(c => c.EndpointsStarted) | ||
.Run(); | ||
|
||
Assert.IsFalse(Endpoint.Context.Decorator.Disposed); | ||
Assert.DoesNotThrow(() => Endpoint.Context.Scope.Dispose()); | ||
} | ||
|
||
class Context : ScenarioContext | ||
{ | ||
public ScopeDecorator Decorator { get; set; } | ||
public ILifetimeScope Scope { get; set; } | ||
} | ||
|
||
class Endpoint : EndpointConfigurationBuilder | ||
{ | ||
public static Context Context { get; set; } | ||
public Endpoint() | ||
{ | ||
EndpointSetup<DefaultServer>(config => | ||
{ | ||
Context = new Context(); | ||
|
||
var container = new ContainerBuilder().Build(); | ||
var scopeDecorator = new ScopeDecorator(container); | ||
|
||
Context.Decorator = scopeDecorator; | ||
Context.Scope = container; | ||
|
||
config.UseContainer<AutofacBuilder>(c => c.ExistingLifetimeScope(scopeDecorator)); | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
namespace NServiceBus.Autofac.Tests | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using global::Autofac; | ||
using global::Autofac.Core; | ||
using global::Autofac.Core.Lifetime; | ||
using global::Autofac.Core.Resolving; | ||
using NServiceBus.ObjectBuilder.Autofac; | ||
using NUnit.Framework; | ||
|
||
[TestFixture] | ||
public class DisposalTests | ||
{ | ||
[Test] | ||
public void Owned_container_should_be_disposed() | ||
{ | ||
var fakeScope = new FakeLifetimeScope(); | ||
|
||
var container = new AutofacObjectBuilder(fakeScope, true); | ||
container.Dispose(); | ||
|
||
Assert.True(fakeScope.Disposed); | ||
} | ||
|
||
[Test] | ||
public void Externally_owned_container_should_not_be_disposed() | ||
{ | ||
var fakeScope = new FakeLifetimeScope(); | ||
|
||
var container = new AutofacObjectBuilder(fakeScope, false); | ||
container.Dispose(); | ||
|
||
Assert.False(fakeScope.Disposed); | ||
} | ||
|
||
sealed class FakeLifetimeScope : ILifetimeScope | ||
{ | ||
public bool Disposed { get; private set; } | ||
|
||
public object ResolveComponent(IComponentRegistration registration, IEnumerable<Parameter> parameters) | ||
{ | ||
return null; | ||
} | ||
|
||
public IComponentRegistry ComponentRegistry { get; } | ||
public void Dispose() | ||
{ | ||
Disposed = true; | ||
} | ||
|
||
public ILifetimeScope BeginLifetimeScope() | ||
{ | ||
return null; | ||
} | ||
|
||
public ILifetimeScope BeginLifetimeScope(object tag) | ||
{ | ||
return null; | ||
} | ||
|
||
public ILifetimeScope BeginLifetimeScope(Action<ContainerBuilder> configurationAction) | ||
{ | ||
return null; | ||
} | ||
|
||
public ILifetimeScope BeginLifetimeScope(object tag, Action<ContainerBuilder> configurationAction) | ||
{ | ||
return null; | ||
} | ||
|
||
public IDisposer Disposer { get; } | ||
public object Tag { get; } | ||
public event EventHandler<LifetimeScopeBeginningEventArgs> ChildLifetimeScopeBeginning; | ||
public event EventHandler<LifetimeScopeEndingEventArgs> CurrentScopeEnding; | ||
public event EventHandler<ResolveOperationBeginningEventArgs> ResolveOperationBeginning; | ||
|
||
private void OnChildLifetimeScopeBeginning(LifetimeScopeBeginningEventArgs e) | ||
{ | ||
ChildLifetimeScopeBeginning?.Invoke(this, e); | ||
} | ||
|
||
private void OnCurrentScopeEnding(LifetimeScopeEndingEventArgs e) | ||
{ | ||
CurrentScopeEnding?.Invoke(this, e); | ||
} | ||
|
||
private void OnResolveOperationBeginning(ResolveOperationBeginningEventArgs e) | ||
{ | ||
ResolveOperationBeginning?.Invoke(this, e); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.