The type 'IUnityContainer' is defined in an assembly that is not referenced

Ken Smith picture Ken Smith · Sep 30, 2015 · Viewed 26.4k times · Source

I just upgraded my ASP.NET MVC/WebApi project from Microsoft.Practices.Unity 3.5.1404 to 3.5.1406 (via nuget, just released). Afterwards, I'm getting this compile error:

Error CS0012 The type 'IUnityContainer' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.Practices.Unity, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

On lines like:

GlobalConfiguration.Configuration.DependencyResolver = 
    new Unity.WebApi.UnityDependencyResolver(container);

Of course, I'm not referencing 3.0.0.0, but 3.5.1.0. So my assumption is that the Unity.WebApi assembly has been compiled against an earlier version of the Microsoft.Practices.Unity assembly. Theoretically, you'd want to fix that with an assembly redirect, like so:

  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Practices.Unity" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.5.1.0" newVersion="3.5.1.0" />
  </dependentAssembly>

However, that doesn't seem to work.

Any suggestions?

Answer

Robert Koch picture Robert Koch · Jan 27, 2016

This might be an more updated answer for how to upgrade from Unity 3.5.1 to 4.0.1.

The type 'IUnityContainer' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.Practices.Unity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

Either via Package Manager Console or NuGet:

  • Uninstall Unity.Mvc4
  • Uninstall Unity.WebAPI
  • Update-Package Unity
  • Install-Package Unity.Mvc (note: no number in package name this time)
  • Install-Package Unity.AspNet.WebApi

Code:

  • Copied container.RegisterTypes from Bootstrapper.cs to App_Start/UnityConfig.cs
  • Exclude Bootstrapper.cs from project
  • Build and Run