Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using TryResolve instead of Resolve with Autofac #413

Merged
merged 1 commit into from Oct 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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