29

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?

Ken Smith
  • 20,305
  • 15
  • 100
  • 147

8 Answers8

30

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
wheaties
  • 35,646
  • 15
  • 94
  • 131
Rob Koch
  • 1,523
  • 3
  • 18
  • 26
12

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.

Ken Smith
  • 20,305
  • 15
  • 100
  • 147
8

I hit this when running a global update-package.

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

user326608
  • 2,210
  • 1
  • 26
  • 33
6

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.

Leigh Shepperson
  • 1,043
  • 5
  • 13
  • You're right, that should have resolved it. But it doesn't seem to have. I was using the old (deprecated?) Unity.WebApi project instead of the newer Unity.AspNet.WebApi project. But even after updating to the correct project, I'm still getting the error. Or a similar one: now it's complaining that it needs version 3.5.0.0. I'm also getting the same error on lines referencing classes in the Microsoft.Practices.Unity.Mvc assembly. – Ken Smith Sep 30 '15 at 21:31
  • 1
    The Unity.WebApi is an open source project that allows Unity IOC integration with MVC projects. However, it does not seem to have been updated since 2013, so I would probably move away from using this. For my projects I use Microsoft's Unity/Unity.MVC, in conjunction with Unity.AspNet.WebApi. I don't seem to have had any issues installing these. – Leigh Shepperson Sep 30 '15 at 22:10
  • Could it be a package manager/ dependency order install issue? Perhaps try out a clean install on a test application? – Leigh Shepperson Sep 30 '15 at 22:12
3

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.

JamesIngold
  • 374
  • 2
  • 8
  • Woah. Unity 3.5.1406 seems to have been removed from Nuget. Interesting. I only see 3.5.1405-prelease, and 4.0. Interesting. I'll try again with the various 4.x flavors (the various associated packages are all updated to that version now) and see what happens. – Ken Smith Oct 01 '15 at 17:17
  • Same here with a web project of mine, I ended up undoing the update as well, gonna wait until MS wake up. – Thorsten Westheider Oct 03 '15 at 09:28
3

A reason for the assembly redirect not working could be a change in the Microsoft.Practices.Unity PublicKeyToken. I've read that between Unity.3.5.1404.0 and Unity.4.0.0 the PublicKeyToken changed from 31bf3856ad364e35 to 6d32ff45e0ccc69f. Maybe it was already changed for 3.5.1406? See: https://github.com/unitycontainer/unity/issues/15

Frank van Eykelen
  • 1,926
  • 1
  • 23
  • 31
2

I just tracked down the latest release of the Unity Container which was Unity 3 on April 2013 and added that to my references.

Radu Bartan
  • 483
  • 6
  • 12
  • Welcome to Stack Overflow! Please, always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Thanks! – dferenc Dec 29 '17 at 21:25
-2

Error 1 The type 'Microsoft.Practices.Unity.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'.

ANSWER-Only for this error just OPEN->Package Manager Console and type

PM> update-package

Debendra Dash
  • 5,334
  • 46
  • 38
  • 2
    Do *NOT* do this. It is a very bad idea. It will update every nuget package in every project in your solution to the latest version. For a large complicated solution, you can't just update every nuget package. Not without scheduling several days (weeks?) of testing time anyway. – mhenry1384 Jun 07 '18 at 00:15