Build error: You must add a reference to System.Runtime
Asked Answered
R

20

237

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.

Rusk answered 2/4, 2014 at 20:32 Comment(0)
R
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
Rusk answered 2/4, 2014 at 20:32 Comment(8)
If you can't find the dll files at the specified folder, you can install Windows SDK as explained: https://mcmap.net/q/119487/-what-install-files-in-location-program-files-x86-reference-assemblies-microsoft-framework-netframework-v4-5Cabana
Thanks. Above link worked after installing 4.5.1 SDK.Karyolymph
There's now a KB article on Microsoft Support that addresses this.Asha
@PeterMajeed You should really post that comment as the answer here. Thanks very much for the tip!Foolish
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. oren.codes/2015/06/16/…Rusk
I had to set 'Copy local' to true in order to make it workTocharian
Encountered the problem with CS-Script CSScript.LoadCode and this was the only way I was able to get it running again.Mundane
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 abovePennell
A
263

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>
Alisaalisan answered 27/7, 2016 at 4:2 Comment(10)
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.Motherless
@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.Albacore
@Albacore 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.Filly
@Filly I replied to your github issue :) github.com/kpi-ua/X.PagedList/issues/49#issuecomment-243515362Albacore
@Albacore 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.Filly
Got this error after installing MoreLINQ into my .Net 4.5.1 project. This Answer solved it.Unipod
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.Recalesce
This answer solved my error. Added the ref in References and this line in web.config.Silt
Got this error when installing into a fresh Win2016 server machine. Fix works correctly.Nereid
@Alisaalisan Thanks for, was able to resolve issue on my WebAppVirge
R
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
Rusk answered 2/4, 2014 at 20:32 Comment(8)
If you can't find the dll files at the specified folder, you can install Windows SDK as explained: https://mcmap.net/q/119487/-what-install-files-in-location-program-files-x86-reference-assemblies-microsoft-framework-netframework-v4-5Cabana
Thanks. Above link worked after installing 4.5.1 SDK.Karyolymph
There's now a KB article on Microsoft Support that addresses this.Asha
@PeterMajeed You should really post that comment as the answer here. Thanks very much for the tip!Foolish
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. oren.codes/2015/06/16/…Rusk
I had to set 'Copy local' to true in order to make it workTocharian
Encountered the problem with CS-Script CSScript.LoadCode and this was the only way I was able to get it running again.Mundane
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 abovePennell
T
47

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>
Timmi answered 14/2, 2018 at 16:2 Comment(3)
Thanks Lorena! Worked for meEntice
working one-liner, does not depend on path to library and does not require to install anything, should be accepted!)Aqueduct
This answer is the same as the top voted answer by @AlisaalisanCockatoo
J
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).

Jato answered 28/4, 2015 at 17:23 Comment(7)
Any ideas what to do with 4.6? I don't see a developer pack around.Indecision
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.Wee
Good questions - I haven't deal with 4.5.2 or 4.6 yet, at least from the build automation standpoint.Jato
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.Rusk
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.Marozik
@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.Jato
I found the developer packs for various .Net versions here: getdotnet.azurewebsites.net/target-dotnet-platforms.htmlCounterstamp
C
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.

Cutanddried answered 30/5, 2016 at 14:1 Comment(1)
This is the answer. Adding to the web.config is a not recommended.Epiboly
E
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.

Escent answered 4/3, 2019 at 10:14 Comment(1)
Solved for me too, inside a similar contextCholine
J
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.

Jaime answered 28/1, 2019 at 15:57 Comment(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/…Gobelin
M
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#

Maria answered 14/10, 2015 at 15:23 Comment(0)
C
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.

Centralize answered 21/12, 2016 at 8:14 Comment(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 projectBiddable
.. and yet it did work, for me also. No choice since the ...\45\... folder only has a stub file in it called _._Neighbor
U
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

Untuck answered 27/1, 2017 at 7:44 Comment(0)
P
2

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

Pedagogics answered 25/12, 2018 at 7:36 Comment(0)
W
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.

Windswept answered 18/9, 2015 at 21:28 Comment(0)
T
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.

Thundering answered 6/3, 2019 at 15:25 Comment(1)
Another way is to add a reference to this assembly in the project and set Copy Local = True.Inaccessible
M
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 .

Masterful answered 17/2, 2022 at 6:25 Comment(0)
F
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>
Fourteenth answered 4/10, 2019 at 14:20 Comment(0)
T
0

For me helped only this code line:

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

Terylene answered 29/6, 2020 at 12:6 Comment(0)
S
0

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

Sotelo answered 13/10, 2022 at 23:27 Comment(0)
L
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>
Loosejointed answered 21/10, 2022 at 9:43 Comment(0)
E
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>
Esther answered 30/11, 2023 at 21:34 Comment(0)
R
-1

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

Radioscopy answered 6/10, 2017 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.