60

I installed a Microsoft ASP.NET Web API Compression nuget package to my project and added a line to WebApiconfig inside Register method as shown in this link https://www.nuget.org/packages/Microsoft.AspNet.WebApi.MessageHandlers.Compression/

 GlobalConfiguration.Configuration.MessageHandlers.Insert(0, new CompressionHandler(new GZipCompressor(), new DeflateCompressor()));

Also added the following code to the 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>

But I am getting an error

Error 1 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'. D:....\App_Start\WebApiConfig.cs

The compiler is complaining about GlobalConfiguration class with error above. I am using >NET Framework 4.5

Karthik Ganesan
  • 4,142
  • 2
  • 26
  • 42
user3772857
  • 601
  • 1
  • 8
  • 8
  • 4
    it's complaining cause it needs a 4.0 version and your project is targeting 4.5 version. – Rahul Jun 24 '14 at 21:49
  • 2
    @Rahul It'd be great to convert your response into an answer, along with instructions for how to solve the issue. – Scott Wegner Oct 06 '14 at 15:48

10 Answers10

69

I got this same error and got around it by doing 2 things.

Background:

I'm building a simple test ASP.Net application to try to work with Google API's OAuth2.0. I used "nuget" to install the Google.Apis.Calendar.v3 (v1.13.1.509) library, and it brought in a lot of other dlls that it depends on.

The Error:

When compiling a seemingly simple ASP.Net project targeting "Framework 4.5" on Visual Studio 2015 for Web (Express). The error showed itself in 2 ways:

I originally compiled the code using the Build command Ctrl+F5. Then I got a build error, but without a entry in the "Error" tab that normally would point to a source code line. (The project just would stop building). The output was:

------ Build started: Project: oauth2.pylogen.com, Configuration: Debug Any CPU ------
Validating Web Site
Building directory '/App_Code/'.

E:\Projects\oauth2.pylogen.com\App_Code\Google.Api.Pylogen\FlowMetadata.cs(26,13): 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'.
Validation Complete
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

I then proceeded by installing the dotNet Framework 4.6.1 Developer Pack (the newest version that is not a preview).

Then I restarted VS 2015 for Web and got build again. This time I got an entry in the "Error" tab, that pointed to a source line:

flowInit.Scopes = new string[] { Google.Apis.Calendar.v3.CalendarService.Scope.Calendar };

When I commented out this line, the project build. I could pretty much do nothing about this line, but I just wanted to show the obscurity of the error!

Solutions:

After installing Framework 4.6.1 Developer Pack. I also added this line 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>

After this my project build completely. My assumption would be that Google is referring to the older System.Runtime.dll (although their Nuget package detail is showing that the target framework is 4.5).

Wasted_Coder
  • 1,878
  • 19
  • 13
  • 6
    I hope you live as long as you like. You are my saviour. – AbbasFaisal Dec 06 '18 at 15:43
  • 3
    Adding 'CsvHelper' via nuget package throws this error. Solved by adding Runtime assembly under compilation tag. – Saravanan Sachi Jul 22 '19 at 09:13
  • 1
    CSVHelper was an issue- Upgraded to 4.7 (so all the projects were the same) did all the updates in NuGet as there was a mix of 4.5 versions. Rebuilt each sub project. The built the website. All good. Thanks @Wasted_Coder you help was much appreciated. – Doug Thompson - DouggyFresh Feb 11 '20 at 12:54
53

This was a bit tricky because I also tried add the assembly reference to the project’s root Web.config file first, which didn’t help in my case. After that I did the same thing for the Web.config file that is located in the Views folder for my ASP.NET MVC project: Views\Web.config

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

The above worked.

Actually I already had one assembly reference System.Web.Mvc there before, so here’s the complete list for my ASP.NET MVC project.

  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </assemblies>
    </compilation>
  </system.web>
jyrkim
  • 2,849
  • 1
  • 24
  • 33
13

Installing and removing System.ValueTuple via Nuget left a System.ValueTuple.dll file in my bin folder. Deleting it solved the problem for me.

Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42
Jonas Äppelgran
  • 2,617
  • 26
  • 30
  • 1
    Moving from System.ValueTuple version 4.3 to 4.5 resolved it. Thanks. – Steve Greene Jul 17 '18 at 15:48
  • Deleting this dll solved it for me too - it had been added in which was causing us issues - thanks! – mjbates7 Sep 26 '19 at 11:38
  • I copied existing deployed folder into new one. Both have this dll but only new one has thrown this error. I deleted ValueTuple.dll and new one works. Don't know how old one works even though ValueTuple.dll exists in that folder. – Saravanan Sachi Feb 10 '20 at 03:30
  • Wow, so this file: System.ValueTuple.dll was causing all these problems. It would have been so difficult to solve this issue if I hadn't found this answer. I had exactly the same situation: install ValueTuple from nuget, then uninstall it cause of some problems and this situation was just there. So difficult to solve, cause you don't even know where to start ... – Manuel Rivera Nov 04 '21 at 21:45
3

It is possible that other files from the .NET Facades folder could also cause the same issue, and would be fixed using the same approach as above for each file in question.

You can find the files in question under the following folder (depending in the target .NET runtime), examples:

.NET 4.5.1 – {ProgramFilesFolder x86 folder} \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.1\Facades
.NET 4.5 – {ProgramFilesFolder x86 folder} \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\Facades

Extracted from https://web.archive.org/web/20150826050219/http://www.lyalin.com/2014/04/25/the-type-system-object-is-defined-in-an-assembly-that-is-not-reference-mvc-pcl-issue/

howcheng
  • 2,211
  • 2
  • 17
  • 24
  • 1
    FYI: The link to www.lyalin.com above returns a database error. Perhaps the link is dead. – qxotk Jun 05 '18 at 14:46
3

I fixed this problem by upgrading C# compiler to Roslyn. You can do it by going to Project - Enable latest C# and VB features for ASP.NET Project.

Alex Sikilinda
  • 2,928
  • 18
  • 34
2

I received this error after a very large update to Nuget, which called for the above error fix for NetStandard. I fixed it like this:

<compilation debug="true" targetFramework="4.7.2">
  <assemblies>
    <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      <add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" />
  </assemblies>
</compilation>
TheWizardOfTN
  • 161
  • 10
2

The solution that worked for me was replacing the compilation node in the root web.config with this:

<system.web>
  <compilation debug="true" targetFramework="4.7.1" >
    <assemblies>
      <add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/>
    </assemblies>
  </compilation>
  <httpRuntime targetFramework="4.7.1" />
...

Be sure to update the targetFramework to whatever you're using.

You can also try editing the csproj and adding this:

<ItemGroup>
  <Reference Include="netstandard" />
</ItemGroup>

Source: https://stackoverflow.com/a/51743098/550975

Serj Sagan
  • 28,927
  • 17
  • 154
  • 183
0

Adding <Reference Include="System.Runtime" /> in csproj file just before <Reference Include="System.Runtime.Serialization" /> has solved my problem.

Suman G
  • 1
  • 1
0

I simply changed the target framework on the solution from .NET Framework 4.5 to .NET Framework 4.6.1 and that did the trick.

0

Got this in a F# project using dotnet 6.0. Did the following to fix:

  • Manually delete bin and obj folder
  • dotnet clean
  • dotnet nuget locals all --clear
  • dotnet restore
  • dotnet build
Peheje
  • 12,542
  • 1
  • 21
  • 30