IUnityContainer.Resolve<T> throws error claiming it cannot be used with type parameters

C#.NetGenericsUnity ContainerCompilation

C# Problem Overview


Yesterday I've implemented the code:

CustomerProductManager productsManager = container.Resolve<CustomerProductManager>();

It was compilable and working.

Today (probably I've modified something) I am constantly getting the error:

> The non-generic method > 'Microsoft.Practices.Unity.IUnityContainer.Resolve(System.Type, > string, params > Microsoft.Practices.Unity.ResolverOverride[])' > cannot be used with type arguments

My collegue has the same source code and doesn't have same error. Why? How to resolve the problem?

P.S.

line "using Microsoft.Practices.Unity;" is present in usings section.

I've tried to replace generic version with non-generic one:

CustomerProductManager productsManager = (CustomerProductManager)container.Resolve(typeof(CustomerProductManager));

And got another error:

> No overload for method 'Resolve' takes > '1' arguments

It seems like one of the assemblies is not referenced.. but which one? I have 2 of them referenced:

  1. Microsoft.Practices.Unity.dll
  2. Microsoft.Practices.ServiceLocation.dll

P.P.S. I've saw similar problem http://unity.codeplex.com/WorkItem/View.aspx?WorkItemId=8205 but it is resolved as "not a bug"

Any thought will be helpful

C# Solutions


Solution 1 - C#

I had the same problem and found the "fix" looking at Prism sample code files. Looks like, even if it is not a dll in Unity V2 you have to add a reference in your class to: Microsoft.Practices.Unity

my complete "using" section is as follow

using System;
using System.Windows;
using Microsoft.Practices.Composite.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.UnityExtensions;

I'm not sure if you are using Silverlight, but the generic version for Container.Resolve IS in Microsoft.Practices.Unity.

Solution 2 - C#

Microsoft no longer owns Unity and it's in version 5, the namespace is now:

using Unity;

Ensure that is in your using section when using:

container.Resolve<T>();

Solution 3 - C#

I faced this problem and none of this answers did not help me. I was getting the compile time error

> Unknown method RegisterType() of > Microsoft.Practices.Unity.IUnityContainer

for my below code.

Container.RegisterType<IMyInterface, MyClass>();

I found that if you did not implement IMyInterface to the class MyClass, you get this issue. Hope it resolves for you too...

Solution 4 - C#

In my situation the class I was wrapping with Unity inherited from an abstract base class, and that base class did NOT have a parameterless constructor. Once I changed my code to use a parameterless constructor for the base class, the problem disappeared.

Solution 5 - C#

In my situation, I had Bootstrapper implement its own Resolve without the generic version, so it couldn't find the Microsoft's Unity Resolve. Adding the proper usings did the trick.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBuddaView Question on Stackoverflow
Solution 1 - C#rodrigoView Answer on Stackoverflow
Solution 2 - C#RandomUs1rView Answer on Stackoverflow
Solution 3 - C#RenjiView Answer on Stackoverflow
Solution 4 - C#Peter HoweView Answer on Stackoverflow
Solution 5 - C#PmanAceView Answer on Stackoverflow