Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The type 'System.Object' is defined in an assembly that is not referenced

Tags:

asp.net

People also ask

Is defined in assembly that is not referenced?

When you get this error, it means that code you are using makes a reference to a type that is in an assembly, but the assembly is not part of your project so it can't use it.

What is an assembly its type its use?

An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. Assemblies take the form of executable (.exe) or dynamic link library (. dll) files, and are the building blocks of . NET applications.


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).


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>

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


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/


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.