unitycontainer / registration-by-convention

Unity.RegistrationByConvention package (Requires Core Implementation. Any contributors?)
Apache License 2.0
4 stars 12 forks source link

Loading Unity assemblies while registering by convention #18

Open deepaknasa opened 4 years ago

deepaknasa commented 4 years ago

I have stumbled upon this issue when i tried to upgrade Unity from 4.0.1 to 5.11.4.

Link to the .NetFiddle

It throws an exception when trying to resolve IContentService var contentSvc = this._container.Resolve<IContentService>();

My investigation has shown that this is occuring because when registering types with Convention it failed to filter out Unity assemblies and reset the mapping for IUnityContainer <--> UnityContainer which was set in UnityContainer constructor.

Further analysis of this issue revealed that this is happening because of missing GlobalAssemblyInfo.cs file from the new version of Unity which was there in Unity 4.0.1. This global assembly had a common AssemblyProductAttribute which is used to know the Unity framework related assemblies and then to filter them out (in class RegistrationByConvention\src\AllClasses.cs).

Whereas in newer version of Unity each assembly has it own product attribute named after it's assembly title. That's where the comparison failed to check IsUnityAssembly. So except RegistrationByConvention assembly rest all are filtered as Non-Unity assemblies (including Unity.Container, Unity.Abstractions which contains UnityContainer and IUnityContainer) thus resetting the mapping for IUnityContainer.

So whenever a service needed IUnityCOntainer it gets the copy of container which doesn't have the mapping of all available types thus throw the exception.

I am happy to fix this one but not sure the right path for it, like should the GlobalAssemblyInfo be back in newer version or Should it filter assemblies based on AssemblyCompanyAttribute instead of AssemblyProductAttribute.

ENikS commented 4 years ago

@deepaknasa It looks like you've figured it all out. Do you want to fix it and send a PR?