234

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Luke Puplett
  • 42,091
  • 47
  • 181
  • 266

19 Answers19

257

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>
Pang
  • 9,564
  • 146
  • 81
  • 122
Baahubali
  • 4,604
  • 6
  • 33
  • 72
  • 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. – Greg Terrell Aug 26 '16 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 Aug 30 '16 at 09:09
  • @Mardoxx I've been having the same issue. Is the problem with VS or X.PagedList? If it's a VS bug, I can patch web.config, but I don't want to alter .config files if the issue is with X.PagedList. – Jecoms Aug 30 '16 at 15:49
  • 1
    @Jecoms I replied to your github issue :) https://github.com/kpi-ua/X.PagedList/issues/49#issuecomment-243515362 – Mardoxx Aug 30 '16 at 17:37
  • @Mardoxx Cheers, mate. I just added the reference as in the answer above. Still don't like it, but it works for now. I already had to manually patch the datepicker I've been using because the current nuget package is broken :P. The downside of dependencies I suppose. – Jecoms Aug 30 '16 at 18:21
  • 3
    Got this error after installing MoreLINQ into my .Net 4.5.1 project. This Answer solved it. – Chris Moschini Nov 26 '17 at 13:57
  • This works, but now I have add another assembly and people might have to add more... it compiles real fine on my computer but does not on Azure. I had to add System.Collections as well. – jsgoupil Jan 23 '18 at 20:02
  • This answer solved my error. Added the ref in References and this line in web.config. – physics90 Apr 16 '18 at 15:07
  • 1
    Got this error when installing into a fresh Win2016 server machine. Fix works correctly. – XaviGuardia Oct 07 '19 at 15:14
  • 1
    @Baahubali Thanks for, was able to resolve issue on my WebApp – Riyaz Shaikh Dec 08 '20 at 02:40
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
Luke Puplett
  • 42,091
  • 47
  • 181
  • 266
  • 2
    If you can't find the dll files at the specified folder, you can install Windows SDK as explained: http://stackoverflow.com/a/14517992/3918598 – user3918598 Aug 12 '14 at 19:26
  • Thanks. Above link worked after installing 4.5.1 SDK. – Matt Beckman Sep 10 '14 at 18:34
  • 43
    There's now a KB article on [Microsoft Support](http://support.microsoft.com/kb/2971005) that addresses this. – Peter Majeed Jan 07 '15 at 15:29
  • 1
    @PeterMajeed You should really post that comment as the answer here. Thanks very much for the tip! – julealgon Mar 16 '15 at 15:26
  • I think this blog post has something to do with the 'facades', my guess is that facades and contract assembly are the same but with inconsistent naming. https://oren.codes/2015/06/16/demystifying-pcls-net-core-dnx-and-uwp-redux/ – Luke Puplett Feb 16 '16 at 10:09
  • 3
    I had to set 'Copy local' to true in order to make it work – Phate01 Sep 14 '18 at 08:31
  • Encountered the problem with CS-Script `CSScript.LoadCode` and this was the only way I was able to get it running again. – Vojtěch Dohnal Jun 26 '19 at 13:49
  • Had the same issue with 4.5.2 on a server and installing the ".NET Framework 4.5.2 Developer Pack" fixed it as suggested in the article linked by @PeterMajeed in the comment above – spilafis Dec 04 '19 at 13:08
43

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>
Pang
  • 9,564
  • 146
  • 81
  • 122
Lorena Pita
  • 1,366
  • 1
  • 17
  • 20
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).

Soviut
  • 88,194
  • 49
  • 192
  • 260
sfuqua
  • 5,797
  • 1
  • 32
  • 33
  • 5
    Any ideas what to do with 4.6? I don't see a developer pack around. – georgiosd Jul 24 '15 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. – abatishchev Jul 31 '15 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 Jul 31 '15 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. – Luke Puplett Nov 26 '15 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. – Mike de Klerk Jun 03 '16 at 09:37
  • @MikedeKlerk do you have all VS updates installed? That does seem very odd - I would have expected that to work. I only used the Developer Pack because I *didn't* have Visual Studio. – sfuqua Jun 03 '16 at 14:43
  • I found the developer packs for various .Net versions here: http://getdotnet.azurewebsites.net/target-dotnet-platforms.html – habakuk Oct 26 '16 at 13:10
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.

wezzix
  • 1,984
  • 1
  • 19
  • 18
12

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Pietro Martinelli
  • 1,776
  • 14
  • 16
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.

Community
  • 1
  • 1
Thomas C. G. de Vilhena
  • 13,819
  • 3
  • 50
  • 44
  • 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: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs1685 – timB33 Mar 30 '21 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#

Oenotria
  • 1,692
  • 11
  • 24
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.

Bob Lokerse
  • 476
  • 6
  • 19
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

Rahbek
  • 1,331
  • 14
  • 9
2

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

Abdullah Tahan
  • 1,963
  • 17
  • 28
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.

sliderhouserules
  • 3,415
  • 24
  • 32
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.

Tomluke
  • 11
  • 1
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 .

Abdullah
  • 983
  • 12
  • 26
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>
Stuart.Sklinar
  • 3,683
  • 4
  • 35
  • 89
0

For me helped only this code line:

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

Balabam
  • 11
  • 4
0

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

Phoebe Buffay
  • 101
  • 2
  • 6
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>
-1

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

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
Stefan
  • 1