Today i came across a seemingly strange issue regarding resolving multiple implementations of a type in combination with property injection of a optional dependency, in my case a logger instance.
I am using Unity 2.1 and UnityConfiguration in order to realize convention-based configuration.
I am using the SetAllPropertiesConvention in order to inject an implementation of ILogger into many of my types.
If i'm resolving a single implementation using for example container.Resolve<IProcessor>()
the instance of ILogger is properly injected.
However, if i'm resolving multiple instances of that type using container.ResolveAll<IProcessor>()
the property remains null.
Here my registration code:
container.RegisterInstance(typeof (ILogger), LoggerService.GetLogger());
container.Configure(c => c.Scan(scan =>
{
scan.AssembliesInBaseDirectory(a => a.FullName.StartsWith("My.Company"));
scan.InternalTypes();
scan.With<AddAllConvention>().TypesImplementing<IFetchDataCommand>();
scan.With<AddAllConvention>().TypesImplementing<IPrintConverter>();
scan.With<SetAllPropertiesConvention>().OfType<ILogger>();
}));
SetAllPropertiesConvention itself uses the following code to register the logger es property injected:
IEnumerable<PropertyInfo> properties =
type.GetProperties().Where(p => p.CanWrite && p.PropertyType == interfaceType);
foreach (PropertyInfo property in properties)
{
registry.Register(null, type).WithInjectionMembers(new InjectionProperty(property.Name));
}
Is this a bug, or did i something wrong. Any ideas?