Skip to content

Commit

Permalink
Make GetService and GetServices methods on AutofacDependencyResolver …
Browse files Browse the repository at this point in the history
…virtual.
  • Loading branch information
alexmg committed Apr 29, 2015
1 parent 041d0f9 commit c3e21a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 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.3.3.{build}
version: 3.3.4.{build}

assembly_info:
patch: true
file: AssemblyInfo.cs
assembly_version: "3.3.0.0"
assembly_file_version: "{version}"
assembly_informational_version: "3.3.3-CI-{build}"
assembly_informational_version: "3.3.4-CI-{build}"

configuration: Release

Expand Down
4 changes: 2 additions & 2 deletions src/Autofac.Integration.Mvc/AutofacDependencyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public ILifetimeScope ApplicationContainer
/// <param name="serviceType">Type of the service.</param>
/// <returns>The single instance if resolved; otherwise, <c>null</c>.</returns>
[SecurityCritical]
public object GetService(Type serviceType)
public virtual object GetService(Type serviceType)
{
return RequestLifetimeScope.ResolveOptional(serviceType);
}
Expand All @@ -160,7 +160,7 @@ public object GetService(Type serviceType)
/// <param name="serviceType">Type of the service.</param>
/// <returns>The list of instances if any were resolved; otherwise, an empty list.</returns>
[SecurityCritical]
public IEnumerable<object> GetServices(Type serviceType)
public virtual IEnumerable<object> GetServices(Type serviceType)
{
var enumerableServiceType = typeof(IEnumerable<>).MakeGenericType(serviceType);
var instance = RequestLifetimeScope.Resolve(enumerableServiceType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Autofac.Integration.Mvc;
Expand Down Expand Up @@ -147,9 +148,11 @@ public void ConfigurationActionInvokedForNestedLifetime()
public void DerivedResolverTypesCanStillBeCurrentResolver()
{
var container = new ContainerBuilder().Build();
var provider = new DerivedAutofacDependencyResolver(container);
DependencyResolver.SetResolver(provider);
Assert.AreEqual(provider, AutofacDependencyResolver.Current, "You should be able to derive from AutofacDependencyResolver and still use the Current property.");
var resolver = new DerivedAutofacDependencyResolver(container);
DependencyResolver.SetResolver(resolver);
Assert.AreEqual(resolver, AutofacDependencyResolver.Current, "You should be able to derive from AutofacDependencyResolver and still use the Current property.");
Assert.That(resolver.GetService(typeof(object)), Is.Not.Null);
Assert.That(resolver.GetServices(typeof(object)), Has.Length.EqualTo(1));
}

[Test]
Expand Down Expand Up @@ -206,9 +209,18 @@ public void GetServicesReturnsRegisteredService()

private class DerivedAutofacDependencyResolver : AutofacDependencyResolver
{
public DerivedAutofacDependencyResolver(IContainer container)
: base(container, new StubLifetimeScopeProvider(container))
public DerivedAutofacDependencyResolver(IContainer container) : base(container)
{
}

public override object GetService(Type serviceType)
{
return serviceType == typeof(object) ? new object() : base.GetService(serviceType);
}

public override IEnumerable<object> GetServices(Type serviceType)
{
return serviceType == typeof(object) ? new[] {new object()} : base.GetServices(serviceType);
}
}
}
Expand Down

0 comments on commit c3e21a7

Please sign in to comment.