Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 285
Ken Smith Avatar asked Sep 30 '15 20:09

Ken Smith


5 Answers

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
like image 186
Robert Koch Avatar answered Nov 14 '22 00:11

Robert Koch


Well, this is probably superfluous now, but the issue apparently had something to do with Unity 3.5.1406, as that got pulled down, and replaced with Unity 4.0. I also replaced the older, apparently unsupported Unity.WebApi library that hasn't been updated in several years, and replaced it with the newer, apparently supported Unity.AspNet.WebApi library. That's a little tricky in NuGet, as a search for "Unity WebApi" returns the older library at the top of the list, and hides the newer library down off the bottom of the first page.

Between all that, it works now, without any issues I've spotted.

like image 12
Ken Smith Avatar answered Nov 13 '22 22:11

Ken Smith


I hit this when running a global update-package.

Rolling back the Unity version via install-package Unity -version 3.5.1404 solved it.

like image 8
user326608 Avatar answered Nov 13 '22 22:11

user326608


You need to make sure you also upgrade to Unity.AspNet.WebApi version 3.5.1406, not just Unity. If you do this, the error should go away.

like image 6
Leigh Shepperson Avatar answered Nov 14 '22 00:11

Leigh Shepperson


I had the same issue this morning, ended up uninstalling v4.0 of Unity along with Unity.WebAPI. Installed v3.5.1405-prelease of Unity and then re-installed Unity.WebAPI.

like image 3
JamesIngold Avatar answered Nov 14 '22 00:11

JamesIngold