Skip to content

Commit

Permalink
Using TryResolve instead of Resolve (#413)
Browse files Browse the repository at this point in the history
  • Loading branch information
StanleyGoldman authored and dpvreony committed Oct 3, 2019
1 parent 5b6187d commit 7c0698c
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/Splat.Autofac/AutofacDependencyResolver.cs
Expand Up @@ -38,16 +38,7 @@ public virtual object GetService(Type serviceType, string contract = null)
{
lock (_lockObject)
{
try
{
return string.IsNullOrEmpty(contract)
? _componentContext.Resolve(serviceType)
: _componentContext.ResolveNamed(contract, serviceType);
}
catch (DependencyResolutionException)
{
return null;
}
return Resolve(serviceType, contract);
}
}

Expand All @@ -59,9 +50,7 @@ public virtual IEnumerable<object> GetServices(Type serviceType, string contract
try
{
var enumerableType = typeof(IEnumerable<>).MakeGenericType(serviceType);
object instance = string.IsNullOrEmpty(contract)
? _componentContext.Resolve(enumerableType)
: _componentContext.ResolveNamed(contract, enumerableType);
var instance = Resolve(enumerableType, contract);
return ((IEnumerable)instance).Cast<object>();
}
catch (DependencyResolutionException)
Expand Down Expand Up @@ -337,6 +326,22 @@ private static bool HasNoContract(Service service)
return !(service is KeyedService);
}

private object Resolve(Type serviceType, string contract)
{
object serviceInstance;

if (string.IsNullOrEmpty(contract))
{
_componentContext.TryResolve(serviceType, out serviceInstance);
}
else
{
_componentContext.TryResolveNamed(contract, serviceType, out serviceInstance);
}

return serviceInstance;
}

private void RemoveAndRebuild(
int registrationCount,
IList<IComponentRegistration> registrations,
Expand Down

0 comments on commit 7c0698c

Please sign in to comment.