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

Error in Generic class with Ienumerable generic parameter #11

Open
luizfbicalho opened this issue Sep 9, 2018 · 4 comments
Open

Error in Generic class with Ienumerable generic parameter #11

luizfbicalho opened this issue Sep 9, 2018 · 4 comments
Assignees
Labels
Bug 🐛 Requires Investigation 👮 Requires further verification

Comments

@luizfbicalho
Copy link

Description

I have an error when I use the unity.configuration that I can't reproduce without the configuration,
I have Interfaces IRepository and IRule
and the Repository class receives an IEnumerable<IRule in the constructor

If I use the unity.config file to register those registrations, I get an error in the resolve of the IRepository.
But if I register by code, I can't reproduce the error, I think that something is missing in the registration, but I can't find what it is.

To Reproduce

Please provide UnitTest in the form of:

class ErrorGenericAndConfig
	{
		[TestMethod]
		static void ErrorWithGenericIEnumerableAndUnityConfig()
		{
			var container = new UnityContainer();

			var unitySection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
			if (unitySection != null)
			{
				unitySection.Configure(container);

			}
			var rep = container.Resolve<IRepository<Entity>>();

			AssertExtensions.IsInstanceOfType(rep, typeof(Repository<Entity>));

			var container2 = new UnityContainer();
			container2.RegisterType(typeof(IRepository<>), typeof(Repository<>),new HierarchicalLifetimeManager());
			container2.RegisterType(typeof(IRule<>), typeof(Rule1<>), "Rule1", new HierarchicalLifetimeManager(),new Unity.Registration.InjectionMember[] { });
			container2.RegisterType(typeof(IRule<>), typeof(Rule2<>), "Rule2", new HierarchicalLifetimeManager(), new Unity.Registration.InjectionMember[] { });
			var rep2 = container2.Resolve<IRepository<Entity>>();

			AssertExtensions.IsInstanceOfType(rep2, typeof(Repository<Entity>));


		}
	}

	public interface IRepository<T>
	{
	}

	public class Repository<T> : IRepository<T>
	{
		public Repository(IEnumerable<IRule<T>> rules)
		{
			this.Rules = rules;
		}

		public IEnumerable<IRule<T>> Rules { get; }
	}

	public interface IRule<T>
	{

	}
	public class Rule2<T> : IRule<T>
	{

	}
	public class Rule1<T> : IRule<T>
	{

	}

	public class Entity
	{
		public int Id { get; set; }
		public string Description { get; set; }
	}

Additional context

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <container> <register type="ErroUnityGeneric.IRepository1, ErroUnityGeneric"
mapTo="ErroUnityGeneric.Repository`1, ErroUnityGeneric"
name="">




	<register type="ErroUnityGeneric.IRule`1, ErroUnityGeneric"
					mapTo="ErroUnityGeneric.Rule1`1, ErroUnityGeneric"
					name="Rule1">
		<lifetime  type="hierarchical"/>
		<constructor >
		</constructor>
	</register>
	<register type="ErroUnityGeneric.IRule`1, ErroUnityGeneric"
					mapTo="ErroUnityGeneric.Rule2`1, ErroUnityGeneric"
					name="Rule2">
		<lifetime  type="hierarchical"/>
		<constructor >
		</constructor>
	</register>

</container>

`

@luizfbicalho
Copy link
Author

I found two differences in the Registration Object

this is the Next property of the registration that doesn't work

"Next": {
"Key": "Unity.Policy.IBuildKeyMappingPolicy, Unity.Abstractions, Version=3.3.1.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f",
"Next": {
"Key": "Unity.Policy.IConstructorSelectorPolicy, Unity.Abstractions, Version=3.3.1.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f",
"Next": null,
"Value": {
}
},

"Value": {
"Name": "Rule1",
"RequireBuild": true,
"Type": "ErroUnityGeneric.Rule1`1, ErroUnityGeneric, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
}
},

And this is the Next property of the registration that work

"Next": {
"Key": "Unity.Policy.IBuildKeyMappingPolicy, Unity.Abstractions, Version=3.3.1.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f",
"Next": null,
"Value": {
"Name": "Rule1",
"RequireBuild": false,
"Type": "ErroUnityGeneric.Rule1`1, ErroUnityGeneric, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
}
},

I changed the registration object after and tryed to resolve the object and It worked for me.

@ENikS
Copy link
Contributor

ENikS commented Oct 1, 2018

You've got so far, do you want to try to fix it? I am a bit swamped at the moment so it might be a while....

@luizfbicalho
Copy link
Author

I tryed to find the most detailed information to help you in this problem, but I don't think that I can solve this because I don't know what those differences in the registration mean.

I think that this error should remain in the radar, but I found a way to solve it in my project.

Thanks

@ENikS ENikS transferred this issue from unitycontainer/unity Dec 3, 2018
@luizfbicalho
Copy link
Author

luizfbicalho commented Jan 21, 2019

I'm trying to help with this issue, but I just hit a dead end here and could use some help.
I didn't find in the unity configuration project where it uses the Unity.Policy.IConstructorSelectorPolicy
Looks like the configuration goes to some Obsolete classes.
Looks like is something about the TypeResolverImpl
It's not clear where this happens.

If @ENikS give me some directions I could help out with this issue

@ENikS ENikS added Bug 🐛 Requires Investigation 👮 Requires further verification labels Apr 3, 2020
@ENikS ENikS added this to the Bugs and Questions milestone Apr 3, 2020
@ENikS ENikS self-assigned this Apr 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 Requires Investigation 👮 Requires further verification
Projects
None yet
Development

No branches or pull requests

2 participants