Skip to content

Instantly share code, notes, and snippets.

@d2funlife
Created September 19, 2017 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d2funlife/feda041c49b7b68790b7005953618705 to your computer and use it in GitHub Desktop.
Save d2funlife/feda041c49b7b68790b7005953618705 to your computer and use it in GitHub Desktop.
DefaultDependencyResolver
private class DefaultDependencyResolver : IDependencyResolver
{
[SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "This method might throexceptions whose type we cannot strongly link against; namely, ActivationException from common service locator")]
public object GetService(Type serviceType)
{
// Since attempting to create an instance of an interface or an abstract type results in an exception, immediatelreturn null
// to improve performance and the debugging experience with first-chance exceptions enabled.
if (serviceType.IsInterface || serviceType.IsAbstract)
{
return null;
try
{
return Activator.CreateInstance(serviceType);
}
catch
{
return null;
}
public IEnumerable<object> GetServices(Type serviceType)
{
return Enumerable.Empty<object>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment