The type ‘System.Object’ is defined in an assembly that is not reference (MVC + PCL issue)

The other day I was working in Visual Studio 2013 on a prototype ASP.NET MVC 5 Project and ran into an unexpected, I found the solution and wanted to share it with the community, so here are the details.

Problem with Razor View & PCL:

The issue was that after I would add a reference to a Portable Class Library in my MVC project the views would stop working with the runtime Exception below:

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0012: The type ‘System.Object’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a’.

At first I was honestly stumped, the project would build fine and would run without issue up until the Razor view was to be rendered, and then it would error out. I could even put a breakpoint in my code (example: controller logic) and hit it, but when the view would try to render it would break.

Solution:
I talked to the nice folks on the ASP.NET & PCL teams at Microsoft where I work and they gave me a solution, in order to fix the Razor view I needed to add a bit of configuration to my web.config file to properly reference the System.Runtime assembly at compilation time.

To implement the fix I first had to expand out the existing web.config compilation section that looks like this by default:

    <compilation debug="true" targetFramework="4.5"/>

Once expanded I then added the following new configuration XML as I was instructed:

      <assemblies>     
        <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />   
      </assemblies>

The final web.config tags should look like this:

    <compilation debug="true" targetFramework="4.5">
      <assemblies>     
        <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />   
      </assemblies>
    </compilation>

And that’s it, this should unblock your project from using the PCL library, the project should run, debug, and render properly.

But why does this happen?

My colleague David Kean answered this on stackoverflow, here is an excerpt:

This is caused by a disconnect and limitation between the build time and the runtime environments of an ASP.NET/MVC project. Most code in a Web project is compiled at build time (ie F5) with exception to views (both Razor and inline ASPX code) which are built dynamically at runtime (ie within IIS).

Source: What does the web.config compilation/assemblies element do? on Stackoverflow

Is System.Runtime the only assembly that can be required?

It is possible that other files from the .NET Facades folder could also cause the same issue, and would be fixed using the same approach as above for each file in question.

You can find the files in question under the following folder (depending in the target .NET runtime), examples:

  • .NET 4.5.1 – {ProgramFilesFolder x86 folder} \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Facades
  • .NET 4.5 – {ProgramFilesFolder x86 folder} \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades

Thanks!

I hope this post helps others and of course feel free to post your comments below.