77

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

Dan J
  • 16,319
  • 7
  • 50
  • 82
Budda
  • 18,015
  • 33
  • 124
  • 206
  • Are you using Unity 2.0? – Vadim May 20 '10 at 18:45
  • 3
    You'll probably find that it's the Power Commands add-on that's removing unused namespace using clauses; the namespace is "unused" when your code is in an uncompilable state and you probably have a habit of saving the file during editing--and the add-on removes the extra uses when saving. – Curt Nichols Jul 30 '10 at 23:31

5 Answers5

176

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.

rodrigo
  • 1,769
  • 1
  • 10
  • 3
  • 9
    Exactly. Some time ago I've detected that adding 'using Microsoft.Practices.Unity;' resolves an issue. – Budda Jul 30 '10 at 23:24
  • 1
    This worked me also, but damn resharper is now moaning about unused 'using directives.' – IbrarMumtaz May 29 '12 at 10:09
  • 6
    The reason is that Resolve with a type parameter is an extension method found in Microsoft.Practices.Unity, while the Resolve() without a type parameter is just a method on the interface (or class). – Geoff Feb 04 '15 at 23:00
  • 1
    Thx! All these years later and this just happened on brand new code. HttpClient is the same way with these 'hidden' extension secrets. Not a fan. I like Simple Injector. – TheDev6 May 20 '16 at 21:13
  • Worked for me. Weird thing was I could go to the definition fine but the using statement wasn't there. – GregP Jun 07 '16 at 15:41
  • 12
    And FWIW, in the new and supposedly highly compatible Unity 5, the namespace is now `Unity` and not `Microsoft.Practices.Unity`. – Ken Smith Oct 24 '17 at 01:33
  • 1
    Thx Ken, you saved me a ton of time trying to track this down. Upgraded my Unity nuget package and this no longer worked until just changing to using Unity like you mentioned – Darthchai Mar 05 '18 at 23:26
  • Thanks, I was totally confused with - Resolve(Type type, string name, params ResolverOverride[] overrides) – Buminda Aug 19 '19 at 01:24
49

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>();
RandomUs1r
  • 4,010
  • 1
  • 24
  • 44
  • 1
    Note that in case you're using Prism, the current stable version (6.3.0) of Prism _still_ uses Unity 4.0.1, where you still have to `using Microsoft.Practices.Unity;` – Ray May 03 '18 at 06:21
  • Seriously, this one was driving me nuts. Thanks! – str8ball Oct 13 '20 at 18:22
1

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...

Martin
  • 2,956
  • 7
  • 30
  • 59
Renji
  • 123
  • 8
0

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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Peter Howe
  • 429
  • 6
  • 19
0

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.

PmanAce
  • 4,000
  • 2
  • 24
  • 29