Build error: You must add a reference to System.Ru

2020-01-24 10:35发布

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.

15条回答
劳资没心,怎么记你
2楼-- · 2020-01-24 11:13

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.

查看更多
戒情不戒烟
3楼-- · 2020-01-24 11:14

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.

查看更多
爷、活的狠高调
4楼-- · 2020-01-24 11:14

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>``
查看更多
SAY GOODBYE
5楼-- · 2020-01-24 11:15

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>
查看更多
Rolldiameter
6楼-- · 2020-01-24 11:21

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.

查看更多
Explosion°爆炸
7楼-- · 2020-01-24 11:23

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.

查看更多
登录 后发表回答