Gabriel Slomka
Gabriel Slomka

Reputation: 1527

Migrating from Unity Container 2.0 to Unity Container 5

So, my app was using Unity Container v2 for dependency injection. Now I'm migrating it to 5+ using NUGET packages, however I'm having issues regarding the classes resolution.

var searchClient = IocContainer.Resolve<DocumentSearch>();

Is giving me

The non-generic method 'IUnityContainer.Resolve(Type, string, params ResolverOverride[])' cannot be used with type arguments App.Super.Web.App D:\Repo\git1601\App.Super.Web.App\API\ApiControllers\DocumentsController.cs

I've changed the imports from Microsoft.Pratices.Unity to Unity only as the packages changed, but still its not working. Any ideas ?

Upvotes: 3

Views: 2250

Answers (1)

Wiktor Zychla
Wiktor Zychla

Reputation: 48279

This works with last Unity downloaded from NuGet (5.3.2)

using Unity;

namespace ConsoleApplication1
{
    public class Foo
    {
    }

    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            var f = container.Resolve<Foo>();
        }
    }
}

Note that Resolve<T> is an extension method and while it is defined in the very same namespace (Unity), your compiler has to support extension methods.

Aren't you compiling this with a very old C#2 compiler where extension methods are not available?

Another possible reason is that you don't have the Unity.Abstractions on your reference list. Note that while UnityContainer type is defined in the Unity.Container assembly, the extension method is defined in the other assembly (installing from NuGet installs both, though).

Make sure both assemblies are referenced in the project you call the extension method, then.

Upvotes: 3

Related Questions