GenesysStack.com

How-to fix: Mvc Error CS0012 The type is definied in an assembly that is not referenced

After deploying a MVC project to the dev web server, a new partial view based on @model List<> began to throw the following exceptions:

·         Error    CS0012            The type 'DateTime' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

·          Error   CS0012            The type 'List<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

The fix was to alter the following block in your Mvc project root web.config:

From:

<compilationdebug="true"targetFramework="4.6" />

To:

<compilationdebug="true"targetFramework="4.6">

      <assemblies>

        <addassembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

        <addassembly="System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

      </assemblies>

    </compilation>

My local environment worked fine. This error only occurs when the project is “Published” via Visual Studio.

In my case, this is caused by the compiler confusing .Net Core/PCL (Profile 151) versus .Net Full 4.6. The Mvc proejct bin folder had different assemblies. My working/local bin folder had System.Runtime.dll and System.Collections.dll. The failing bin folder had no System.Runtime.dll, but had System.Collections.Immutability.dll. The Mvc project did not explicitly reference any of those dependencies, it was most likely pulled in at build time via the Roslyn compiler.

Leave your comment