Skip to content

Commit

Permalink
Resolved issue #3 - Made sure AutofacInstanceProvider disposes the li…
Browse files Browse the repository at this point in the history
…fetime scope immediately when an exception is thrown in the constructor of the service being resolved.
  • Loading branch information
alexmg committed Jun 17, 2015
1 parent b250c1d commit 9323aa1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
version: 3.0.1.{build}
version: 3.0.2.{build}

assembly_info:
patch: true
file: AssemblyInfo.cs
assembly_version: "3.0.0.0"
assembly_file_version: "{version}"
assembly_informational_version: "3.0.1-CI-{build}"
assembly_informational_version: "3.0.2-CI-{build}"

configuration: Release

Expand Down
17 changes: 14 additions & 3 deletions src/Autofac.Integration.Wcf/AutofacInstanceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,20 @@ public object GetInstance(InstanceContext instanceContext, Message message)
{
throw new ArgumentNullException("instanceContext");
}
var extension = new AutofacInstanceContext(_rootLifetimeScope);
instanceContext.Extensions.Add(extension);
return extension.Resolve(_serviceData);

var autofacInstanceContext = new AutofacInstanceContext(_rootLifetimeScope);
instanceContext.Extensions.Add(autofacInstanceContext);

try
{
return autofacInstanceContext.Resolve(_serviceData);
}
catch (Exception)
{
autofacInstanceContext.Dispose();
instanceContext.Extensions.Remove(autofacInstanceContext);
throw;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Autofac;
using Autofac.Core;
using Autofac.Integration.Wcf;
using Autofac.Util;
using NUnit.Framework;

namespace Autofac.Tests.Integration.Wcf
Expand Down Expand Up @@ -43,6 +47,23 @@ public void ReleaseInstance_NullInstanceContext()
Assert.Throws<ArgumentNullException>(() => provider.ReleaseInstance(null, instance));
}

[Test]
public void LifetimeScopeDisposedWhenExceptionThrownInServiceConstructor()
{
var builder = new ContainerBuilder();
var released = false;
builder.RegisterType<Disposable>().OnRelease(d => released = true);
builder.RegisterType<BadService>();
var container = builder.Build();
var data = new ServiceImplementationData {ImplementationResolver = l => l.Resolve<BadService>()};
var provider = new AutofacInstanceProvider(container, data);
var context = new InstanceContext(new object());

Assert.Throws<DependencyResolutionException>(() => provider.GetInstance(context));

Assert.IsTrue(released);
}

private class TestMessage : Message
{
public override MessageHeaders Headers
Expand All @@ -65,5 +86,14 @@ public override MessageVersion Version
get { throw new NotImplementedException(); }
}
}

class BadService
{
// ReSharper disable once UnusedParameter.Local
public BadService(Disposable disposable)
{
throw new Exception("Boom!!!");
}
}
}
}

0 comments on commit 9323aa1

Please sign in to comment.