Skip to content

Commit

Permalink
Merge pull request #341 from eventflow/fix-autofac-validation-registr…
Browse files Browse the repository at this point in the history
…ations

When validating registrations, a lifetime scope should be used
  • Loading branch information
rasmus authored May 29, 2017
2 parents af71dde + e106433 commit 974a039
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
5 changes: 4 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
### New in 0.46 (not released yet)

* _Nothing yet_
* Fix: EventFlow now uses a Autofac lifetime scope for validating service
registrations when `IEventFlowOpions.CreateResolver(true)` is invoked.
Previously services were created but never disposed as they were resolved
using the root container

### New in 0.45.2877 (released 2017-05-28)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,49 @@
using EventFlow.Configuration;
using EventFlow.TestHelpers;
using EventFlow.TestHelpers.Suites;
using FluentAssertions;
using Moq;
using NUnit.Framework;

namespace EventFlow.Autofac.Tests.UnitTests
{
[Category(Categories.Unit)]
public class AutofacServiceRegistrationTests : TestSuiteForServiceRegistration
{
[Test]
public void ValidateRegistrationsShouldDispose()
{
// Arrange
var service = new Mock<I>();
var createdCount = 0;
Sut.Register(_ =>
{
createdCount++;
return service.Object;
});

// Act and Assert
using (var resolver = Sut.CreateResolver(true))
{
createdCount.Should().Be(1);
service.Verify(m => m.Dispose(), Times.Once);

var resolvedService = resolver.Resolve<I>();
createdCount.Should().Be(2);
resolvedService.Should().BeSameAs(service.Object);

using (var scopedResolver = resolver.BeginScope())
{
var nestedResolvedService = scopedResolver.Resolve<I>();
createdCount.Should().Be(3);
nestedResolvedService.Should().BeSameAs(service.Object);
}
service.Verify(m => m.Dispose(), Times.Exactly(2));
}

service.Verify(m => m.Dispose(), Times.Exactly(3));
}

protected override IServiceRegistration CreateSut()
{
return new AutofacServiceRegistration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public void AlwaysUnique()
Sut.Register<I, A>();

// Act
var resolver = Sut.CreateResolver(true);
var resolver = Sut.CreateResolver(false);
var i1 = resolver.Resolve<I>();
var i2 = resolver.Resolve<I>();

Expand Down
19 changes: 11 additions & 8 deletions Source/EventFlow/Extensions/ResolverExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ namespace EventFlow.Extensions
public static class ResolverExtensions
{
public static void ValidateRegistrations(
this IResolver resolver)
this IRootResolver resolver)
{
var exceptions = new List<Exception>();
foreach (var type in resolver.GetRegisteredServices())
using (var scopeResolver = resolver.BeginScope())
{
try
foreach (var type in scopeResolver.GetRegisteredServices())
{
resolver.Resolve(type);
}
catch (Exception ex)
{
exceptions.Add(ex);
try
{
scopeResolver.Resolve(type);
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
}

Expand Down

0 comments on commit 974a039

Please sign in to comment.