31
votes

I created a Windows Form application in Visual Studio 2012 on Windows 8, and the target .NET framework is 4.5, and I want to use the Datagramsocket library of Windows SDK, so I changed the target OS version to 8, when I compile.

I am getting the following error:

"Error 1 The type 'System.IDisposable' 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'."

9
Wince. If I were you I'd consider having a dll to wrap 4.0 components up and using that from your 4.5 code just to keep which System is which all neat and tidy.Tony Hopkinson
@TonyHopkinson 4.5 is an in-place upgrade, so you're always going to be using the 4.5 System.David Pfeffer
@David, This datagram thingy doesn't agree with you, I'm happy to though. :)Tony Hopkinson

9 Answers

31
votes

If you are targeting Windows 8, the MSDN documentation has the following instructions:

That said, your desktop app can’t consume much of anything from the Windows Runtime until you prepare your project with one essential reference. The Windows Runtime defines some standard classes and interfaces in System.Runtime, such as IEnumerable, that are used throughout the Windows Runtime libraries. By default, your managed desktop app won’t be able to find these types, and so you must manually reference System.Runtime before you can do anything meaningful with Windows Runtime classes. To create this manual reference:

  1. Navigate to your managed desktop app project in the Solution Explorer.
  2. Right-click the References node and click Add Reference.
  3. Click the Browse tab.
  4. Click Browse….
  5. Navigate to the System.Runtime.dll façade. You can generally find this in a path similar to: %ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.dll

Hope that helps.

19
votes

If you run into this problem with 4.5.2 and Unity, Microsoft has issued a fix, KB article: http://support.microsoft.com/kb/2971005

2
votes

I just ran into this issue with Windows 10, TeamCity and VS2015:

Symptoms:

Visual studio builds the project fine, but TeamCity struggles with compile issues.

Reason:

Added a new project to existing solution - Forgot to change the TargetRuntimeVersion.

Solution:

Unload project

Edit .csproj file for that project

Change:

<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>

To whatever you other projects are, or whatever is installed on the server.

For me it was:

<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>

Hope this helps anyone else experiencing this.

1
votes

It looks like your app is trying to load System.Runtime version 4.0, but that you don't have that version installed. You say you have .NET 4.5 installed, so you can provide some configuration to redirect assembly bindings at runtime.

Try adding this to your App.config or Web.config file:

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" />
      <bindingRedirect oldVersion="4.0.0.0" newVersion="4.5.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

I haven't tested this exact scenario, but this configuration has helped me in similar situations.

1
votes

While working with a TFS build Agent I kept seeing this error below but only on a Windows 2012 R2 server with 4.5.2 installed. None of our Visual Studio machine ever had the compile error.

"Error 1 The type 'System.IDisposable' 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'."

The cause as others have pointed out has to do with Portable Class Libraries and as others have pointed out. but as for a solution some recommend a reference to the System.Runtime.dll facade in %ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades\System.Runtime.dll I disagree as the path and version may be different on other machines.

Instead the best solution I've found was to install the Windows SDK on the build machine.

I found this solution from a post here here and a really clear description as to why this problem occurs here

1
votes

I had this error message in TeamCity after I added Unity 4.0.1 to the project that is targeting .NET Framework 4.6.1. I ended up copying missing folders in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework from my development machine to the build server. Both are running Windows 7. After that build succeeded.

enter image description here

1
votes

Based on TFS 2015 Build Server

I do not know if this error message is generic and the answers/solution may be unique for each unique error. But I had similar error the type 'System.IDisposable' 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'

I realised that the project was using Microsoft.Practices.Unity and had interface IUnityContainer which implements IDisposable. It turned out that the TFS Build Definition NuGet Installer Build Step was not configured, which is responsible for restoring Microsoft.Practices.Unity NuGet package on the TFS Build Server. If you come across this error, please check your TFS Build Definition and point the NuGet Installer Build Step Path to your solution full path, including the .sln extension

enter image description here

0
votes

As Josh mentions, I had this problem in a solution with a Web API project and several library projects, where one of the library projects was borking on build, with errors that said the Unity attributes weren't "valid" attributes, and then one of the errors said The type 'System.Attribute' is defined in an assembly that is not referenced. You must add a reference to assembly System.Runtime.

The suggested "updated" 4.5.2 Developer Pack is what I already had installed, so that didn't help me. On a hunch I thought 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.

0
votes

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

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.