Should IDependencyResolver.GetServices using Unity return the default (unnamed) registration
I've a question about the implementation of MVC's IDependencyResolver.GetServices when using Unity for the dependency injection.
There are several example implementations to be found, most of which have been implemented to work around the problem of Unity throwing exceptions when trying to resolve unregistered types.
Usualy GetServices is implemented like this:
IEnumerable<object> IDependencyResolver.GetServices(Type serviceType)
{
try
{
return _container.ResolveAll(serviceType);
}
catch
{
return new List<object>();
}
}
What is of concern to me, and I have been unable to find any confirmation either开发者_开发知识库 way, is that ResolveAll does not return the default (unnamed) registration. See here.
The documentation for IDependencyResolver does not make the same statement.
Does anyone know whether IDependencyResolver.GetServices should return all registered instances or just the named instances (using Unity parlance)?
Try below code. I hope this will work.
return (serviceType.IsClass && !serviceType.IsAbstract) || _container.IsRegistered(serviceType) ? _container.ResolveAll(serviceType) : new object[] {};
精彩评论