238

I'm preparing a brand new ASP.NET MVC 5.1 solution. I'm adding in a bunch of NuGet packages and setting it up with Zurb Foundation, etc.

As part of that, I've added a reference to an in-house NuGet package which is a Portable Class Library and I think this is causing a problem on the build server.

TeamCity fails the build with:

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

I originally added the fix for the same or similar error when compiling the Razor web pages, that fix being in the web.config

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

However, the issue is unresolved.

20 Answers 20

265

To implement the fix, first 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>
10
  • 2
    This issue crept into a MVC project targeting 4.52, no known PCL but many nuGet packages. I believe that uninstalling VS2013 triggered this but I do not know that as fact. Error only surfaced in Views that referenced X-PagedList. Adding reference to project web.config (not views web.config) fixed it. Commented Aug 26, 2016 at 18:33
  • 2
    @GregTerrell I just had this exact issue after upgrading from an old version of X.PagedList. So frustrating. Adding the reference worked, then what I did was deleted (renamed) the .vs folder after closing the project, reopened it, removed the web.config entry and now it builds and runs fine.
    – Mardoxx
    Commented Aug 30, 2016 at 9:09
  • 1
    @Jecoms I replied to your github issue :) github.com/kpi-ua/X.PagedList/issues/49#issuecomment-243515362
    – Mardoxx
    Commented Aug 30, 2016 at 17:37
  • 3
    Got this error after installing MoreLINQ into my .Net 4.5.1 project. This Answer solved it. Commented Nov 26, 2017 at 13:57
  • 1
    Got this error when installing into a fresh Win2016 server machine. Fix works correctly. Commented Oct 7, 2019 at 15:14
132

Adding a reference to this System.Runtime.dll assembly fixed the issue:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Facades\System.Runtime.dll

Though that file in that explicit path doesn't exist on the build server.

I will post back with more information once I've found some documentation on PCL and these Facades.

Update

Yeah pretty much nothing on facade assemblies on the whole internet.

Google:

(Facades OR Facade) Portable Library site:microsoft.com
8
  • 2
    If you can't find the dll files at the specified folder, you can install Windows SDK as explained: stackoverflow.com/a/14517992/3918598 Commented Aug 12, 2014 at 19:26
  • Thanks. Above link worked after installing 4.5.1 SDK. Commented Sep 10, 2014 at 18:34
  • 43
    There's now a KB article on Microsoft Support that addresses this. Commented Jan 7, 2015 at 15:29
  • 1
    @PeterMajeed You should really post that comment as the answer here. Thanks very much for the tip!
    – julealgon
    Commented Mar 16, 2015 at 15:26
  • 3
    I had to set 'Copy local' to true in order to make it work
    – Phate01
    Commented Sep 14, 2018 at 8:31
48

The only way that worked for me - add the assembly to web.config

<compilation debug="true" targetFramework="4.5">
  <assemblies>     
    <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />   
  </assemblies>
</compilation>
3
  • 1
    Thanks Lorena! Worked for me Commented Dec 24, 2018 at 18:34
  • working one-liner, does not depend on path to library and does not require to install anything, should be accepted!) Commented Jan 4, 2019 at 14:58
  • 2
    This answer is the same as the top voted answer by @Baahubali Commented Jan 11, 2019 at 11:58
28

@PeterMajeed's comment in the accepted answer helped me out with a related problem. I am not using the portable library, but have the same build error on a fresh Windows Server 2012 install, where I'm running TeamCity.

Installing the Microsoft .NET Framework 4.5.1 Developer Pack took care of the issue (after having separately installed the MS Build Tools).

7
  • 5
    Any ideas what to do with 4.6? I don't see a developer pack around.
    – georgiosd
    Commented Jul 24, 2015 at 13:20
  • 4
    I have both 4.5.1 and 4.5.2 dev packs and still getting this error. Ideas? I don't want to reference it in all my 25 projects. By some reasons other 25 don't require it. Commented Jul 31, 2015 at 16:54
  • Good questions - I haven't deal with 4.5.2 or 4.6 yet, at least from the build automation standpoint.
    – sfuqua
    Commented Jul 31, 2015 at 16:57
  • I guess if you're really stuck, you could always pop the free Visual Studio Community Service Edition on and keep it up-to-date. Commented Nov 26, 2015 at 10:18
  • I don't understand why these developer packs have to be installed. I installed VS2013 Pro. Isn't that enough? But it solved the problem here. Thanks. Commented Jun 3, 2016 at 9:37
16

I had this problem in some solutions on VS 2015 (not MVC though), and even in the same solution on one workstation but not on another. The errors started appeared after changing .NET version to 4.6 and referencing PCL.

The solution is simple: Close the solution and delete the hidden .vs folder in the same folder as the solution.

Adding the missing references as suggested in other answers also solves the problem, but the error remains solved even after you remove the references again.

As for TeamCity, I cannot say since my configuration never had a problem. But make sure that you reset the working catalog as a part of your debugging effort.

1
  • This is the answer. Adding to the web.config is a not recommended.
    – Mark Bad
    Commented Oct 11, 2021 at 14:40
13

It's an old issue but I faced it today in order to fix a build pipeline on our continuous integration server. Adding

<Reference Include="System.Runtime" />

to my .csproj file solved the problem for me.

A bit of context: the interested project is a full .NET Framework 4.6.1 project, without build problem on the development machines. The problem appears only on the build server, which we can't control, may be due to a different SDK version or something similar.

Adding the proposed <Reference solved the build error, at the price of a missing reference warning (yellow triangle on the added entry in the references tree) in Visual Studio.

1
  • Solved for me too, inside a similar context
    – Nicola C.
    Commented Oct 16, 2019 at 13:07
4

I was also facing this problem trying to run an ASP .NET MVC project after a minor update to our codebase, even though it compiled without errors:

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'.

Our project had never run into this problem, so I was skeptical about changing configuration files before finding out the root cause. From the error logs I was able to locate this detailed compiler output which pointed out to what was really happening:

warning CS1685: The predefined type 'System.Runtime.CompilerServices.ExtensionAttribute' is defined in multiple assemblies in the global alias; using definition from 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll'

c:\Users\Admin\Software Development\source-control\Binaries\Publish\WebApp\Views\Account\Index.cshtml(35,20): error 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'.

c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\meseems.webapp\68e2ea0f\8c5ee951\assembly\dl3\52ad4dac\84698469_3bb3d401\System.Collections.Immutable.DLL: (Location of symbol related to previous error)

Apparently a new package added to our project was referencing an older version of the .NET Framework, causing the "definition in multiple assemblies" issue (CS1685), which led to the razor view compiler error at runtime.

I removed the incompatible package (System.Collections.Immutable.dll) and the problem stopped occurring. However, if the package cannot be removed in your project you will need to try Baahubali's answer.

1
  • IMO this is the better fix. Some more info on CS1685 "This error occurs when a predefined system type such as System.int32 is found in two assemblies. One way this can happen is if you are referencing mscorlib from two different places, such as trying to run the .NET Framework versions 1.0 and 1.1 side-by-side." from: learn.microsoft.com/en-us/dotnet/csharp/language-reference/…
    – timB33
    Commented Mar 30, 2021 at 10:52
3

Install the .NET Runtime as well as the targeting pack for the .NET version you're targeting.

The developer pack is just these two things bundled together but as of today doesn't seem to have a 4.6 version so you'll have to install the two items separately.

Downloads can be found here: http://blogs.msdn.com/b/dotnet/p/dotnet_sdks.aspx#

2

On our Tfs 2013 build server I had the same error, in a test project. with the main web project running on .Net 4.5.1.

I installed a nuGet package of System Runtime and added the reference from packages\System.Runtime.4.3.0\ref\net462\System.Runtime.dll

That solved it for me.

2
  • That probably shouldn't work since you're telling it to use the .NET 4.6.2 DLL with a .NET 4.5.1 project Commented Jul 7, 2017 at 21:19
  • 1
    .. and yet it did work, for me also. No choice since the ...\45\... folder only has a stub file in it called _._ Commented Jul 20, 2017 at 8:15
2

I needed to download and install the Windows 8.0 (and not 8.1) SDK to make the error disappear on my TeamCity server.

https://developer.microsoft.com/en-us/windows/downloads/windows-8-sdk

2

i added System.Runtime.dll to bin project and it worked :)

1

I had this problem in a solution with a Web API project and several library projects. One of the library projects was borking on build, with errors that said the Unity attributes weren't "valid" attributes, and then one error said I needed to reference System.Runtime.

After much searching, reinstalling the 4.5.2 Developer Pack, and nothing working, I figured maybe it was just a version mismatch. So I looked at the properties of every project, and one of the very base libraries was targeting 4.5 while every other one was targeting 4.5.2. I changed that one to also target 4.5.2 and the errors went away.

1

I copy the file "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5.1\Facades\system.runtime.dll" to bin folder of production server, this solve the problem.

1
  • 1
    Another way is to add a reference to this assembly in the project and set Copy Local = True.
    – John Mc
    Commented Sep 10, 2019 at 23:36
1

For me I am using Microsoft visual studio 2019 and Windows server 2019 .

This web.config compilation part

<compilation debug="true" targetFramework="4.7.2">

Suddenly this error appeared during development and coding

I tried adding the assembly and reference its not solved the issue This error solved after close and open visual studio and open project again .

0

install https://www.microsoft.com/en-us/download/details.aspx?id=49978 Microsoft .NET Framework 4.6.1 Developer Pack and add this line of code in Web.config file

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

For me helped only this code line:

Assembly.Load("System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

0

Deleting the bin folder and rebuilding the solution worked for me.

0

In my case the .csproj file somehow was mixed up. Some 'Compile' elements were missing a 'SubType'.

<Compile Include="Control.cs" />

Fixed the issue by adding the "SubType" again:

<Compile Include="Control.cs">
  <SubType>UserControl</SubType>
</Compile>
0

Adding this definitely resolves this issue. Make sure the target frame work matches yours. For instance; 4.5.2

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

Removing the reference over the Nuget Package Manager and re-adding it solved the problem for me.

Not the answer you're looking for? Browse other questions tagged or ask your own question.