diff --git a/AutofacContrib.NSubstitute.Tests/AutofacContrib.NSubstitute.Tests.csproj b/AutofacContrib.NSubstitute.Tests/AutofacContrib.NSubstitute.Tests.csproj
index 71c7358..6797d50 100644
--- a/AutofacContrib.NSubstitute.Tests/AutofacContrib.NSubstitute.Tests.csproj
+++ b/AutofacContrib.NSubstitute.Tests/AutofacContrib.NSubstitute.Tests.csproj
@@ -55,6 +55,7 @@
+
diff --git a/AutofacContrib.NSubstitute.Tests/KeyedRegistrationFixture.cs b/AutofacContrib.NSubstitute.Tests/KeyedRegistrationFixture.cs
new file mode 100644
index 0000000..c7fa461
--- /dev/null
+++ b/AutofacContrib.NSubstitute.Tests/KeyedRegistrationFixture.cs
@@ -0,0 +1,63 @@
+using Autofac.Features.Indexed;
+using NSubstitute;
+using NUnit.Framework;
+
+namespace AutofacContrib.NSubstitute.Tests
+{
+ public enum Switch
+ {
+ Off,
+ On
+ }
+
+ public class ClassWithKeyedDependencies
+ {
+ public ClassWithKeyedDependencies(IIndex dependencies)
+ {
+ OnDependency = dependencies[Switch.On];
+ OffDependency = dependencies[Switch.Off];
+ }
+
+ public IDependency2 OnDependency { get; private set; }
+ public IDependency2 OffDependency { get; private set; }
+ }
+
+ public static class KeyedRegistrationFixture
+ {
+ [Test]
+ public static void ShouldResolveIndexedDependencies()
+ {
+ var autoSubstitute = new AutoSubstitute();
+
+ var target = autoSubstitute.Resolve();
+
+ Assert.NotNull(target.OnDependency);
+ Assert.NotNull(target.OffDependency);
+ }
+
+ [Test]
+ public static void ShouldResolveASubstituteForIndexedDependency()
+ {
+ var autoSubstitute = new AutoSubstitute();
+ var index = autoSubstitute.Resolve>();
+ index[Switch.On].SomeOtherMethod().Returns(5);
+
+ var target = autoSubstitute.Resolve();
+
+ Assert.That(target.OnDependency.SomeOtherMethod(), Is.EqualTo(5));
+ }
+
+ [Test]
+ public static void ShouldAcceptProvidedIndexedDependency()
+ {
+ var autoSubstitute = new AutoSubstitute();
+ var substitute = Substitute.For();
+ substitute.SomeOtherMethod().Returns(5);
+ autoSubstitute.Provide(substitute, Switch.On);
+
+ var target = autoSubstitute.Resolve();
+
+ Assert.That(target.OnDependency.SomeOtherMethod(), Is.EqualTo(5));
+ }
+ }
+}
diff --git a/AutofacContrib.NSubstitute/AutoSubstitute.cs b/AutofacContrib.NSubstitute/AutoSubstitute.cs
index 069526f..95bccd1 100644
--- a/AutofacContrib.NSubstitute/AutoSubstitute.cs
+++ b/AutofacContrib.NSubstitute/AutoSubstitute.cs
@@ -100,6 +100,23 @@ public TService Provide(TService instance)
return Container.Resolve();
}
+ ///
+ /// Register the specified object to the container as the specified keyed service type and resolve it.
+ ///
+ /// The type to register the object as
+ /// The object to register into the container
+ /// The key to register the service with
+ /// The instance resolved from container
+ public TService Provide(TService instance, object serviceKey)
+ where TService : class
+ {
+ Container.ComponentRegistry.Register(RegistrationBuilder.ForDelegate((c, p) => instance).As(new KeyedService(serviceKey, typeof(TService)))
+ .InstancePerLifetimeScope().CreateRegistration()
+ );
+
+ return Container.Resolve();
+ }
+
///
/// Registers to the container and returns a substitute for a given concrete class given the explicit constructor parameters.
/// This is used for concrete classes where NSubstitutes won't be created by default by the container when using Resolve.
diff --git a/AutofacContrib.NSubstitute/NSubstituteRegistrationHandler.cs b/AutofacContrib.NSubstitute/NSubstituteRegistrationHandler.cs
index 387bc15..ffb62a8 100644
--- a/AutofacContrib.NSubstitute/NSubstituteRegistrationHandler.cs
+++ b/AutofacContrib.NSubstitute/NSubstituteRegistrationHandler.cs
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Linq;
using Autofac;
using Autofac.Builder;
using Autofac.Core;
+using Autofac.Features.Indexed;
using NSubstitute;
namespace AutofacContrib.NSubstitute
@@ -25,8 +27,8 @@ public IEnumerable RegistrationsFor
{
if (service == null)
throw new ArgumentNullException("service");
-
- var typedService = service as TypedService;
+
+ var typedService = service as IServiceWithType;
if (typedService == null ||
!typedService.ServiceType.IsInterface ||
typedService.ServiceType.IsGenericType && typedService.ServiceType.GetGenericTypeDefinition() == typeof(IEnumerable<>) ||