Type 'Object' is defined in an assembly that is not referenced (NET Standard 2.0/.NET Framework 4.6.1) [duplicate]
Asked Answered
L

10

51

I'm using the .NET Standard 2.0 preview, on which my Class Libraries are based.

After having trouble with a few NuGet packages, especially regarding archive extraction, I decided to migrate my .NET Core 2.0 Console projects back to the .NET Framework 4.6.1.

The .NET Framework 4.6.1 is supposed to implement the .NET Standard 2.0 specification - according to different sources. Especially the dotnet/standard GitHub Repo.

Net Standard Implementation by Net Framework and Core

Unfortunately, the migration to the .NET Framework resulted in the following errrors throughout all of .NET Framework Console projects:

Error CS0012 The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Where Object can be anything: Enum, Task, ...

How would I reference .NET Standard 2.0 class libraries with .NET Framework (4.6.1) without getting such errors?

Lalia answered 20/6, 2017 at 11:58 Comment(1)
We should not be changing the config file. I refrained from changing .config file in this case. I changed the Microsoft.NetFramework.CurrentVersion.targets file in C:\Program Files (x86)\MSBuild\14.0\Bin to include the lines below my code started building fine <ItemGroup> <Reference Include="netstandard" /> </ItemGroup>Internee
E
47

Try to add a reference to netstandard in web.config as below:

<system.web> 
   <compilation debug="true" targetFramework="4.7.1"> 
      <assemblies> 
         <add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/> 
      </assemblies> 
    </compilation> 
</system.web>
Eula answered 17/9, 2018 at 5:49 Comment(1)
Thanks, this saved me loads of timeDittography
G
29

I had this problem even after using the latest 2.0, and VS 15.3. However, I think my problem was different. After upgrading from Core 1.1, to 2.0, for some reason, my .web's .csproj had <RuntimeFrameworkVersion>1.1.2</RuntimeFrameworkVersion>. Which prevented the project from targeting the correct 2.0 version.

My class libraries (.Layer) projects had <NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>.

I deleted both of them and my project finally started using 2.0 and everything went fine after.

Gregarious answered 21/8, 2017 at 8:39 Comment(4)
I had same doubt about RuntimeFrameworkVersion, after removing it, errors goneMatherne
Correct, I had a very similar problem. I have used this tutorial for migrating my project from 1.1 to 2.0 but it failed to compile with a bunch of the same error messages as the OP reported. Finally I had to modify RuntimeFrameworkVersion from 1.6.1 to 2.0 and the problem was solved. Note that I did not delete this node.Lashondra
@Lashondra glad it worked for you without deleting it. However, in my opinion, you are saying you want to use 2.0 on your project. So, if you update it to... say 2.1 in the future, you might run into the same problem. I believe if you delete RuntimeFrameworkVersion, .Core will use the latest available version.Gregarious
@Lashondra I had the same problem you had. I just deleted this tag and it worked.Copal
L
13

After installing NET Core 2.0 preview 2 and updating to the latest version of Visual Studio 2017 Preview (15.3), the references are now automatically installed.

According to this GitHub issue, dealing with a similar problem, you have to manually add a reference to the NETStandard.Library.NETFramework package within your .NET Framework project for now*.

Either install it via NuGet Console:

Install-Package NETStandard.Library.NETFramework -Version 2.0.0-preview1-25305-02 -Pre

A few days ago, the NET Core/Standard 2.0 preview 2 was released, if you updated, the following version is needed:

Install-Package NETStandard.Library.NETFramework -Version 2.0.0-preview2-25405-01 -Pre

or via the NuGet store (check Show Pre-release versions) and search for NETStandard.Library.NETFramework

This will then resolve the references, the errors should vanish.

*Joperezr states that Microsoft is planning to let a tool handle this later.

For now you have to manually add a reference to this package which can be annoying, but in the future we are planning on the tooling doing this for you.

Lalia answered 20/6, 2017 at 11:58 Comment(7)
I don't know, whether I'm the only one with that problem, so I thought I'd share this by self-answeringLalia
this should be fixed in the upcoming preview2 release and a VS 15.3 preview update that include the necessary logic, no longer requiring this nuget packageThough
@MartinUllrich: All right, thanks for the info. Then my answer is (nearly/already) outdated ... I'll keep the question open until the new release of the VS preview and delete it then.Lalia
Valid question for .NET Core/Standard 2.0 release version as well. Error is present when building with script using msbuild. Compiles fine with VS 2017 (15.3), though. Library targeting net461 should be able to use netstandard2.0 libraries but is not.Charil
As far as I can tell, this is still necessary to build on a CI server without full Visual Studio installed.Asper
Even after adding the reference to above package to all projects in solution I get lot of this kind of issues Multiple assemblies with equivalent identity have been imported: 'D:\Projects\3.1.2\src\packages\NETStandard.Library.NETFramework.2.0.0-preview2-25405-01\build\net461\ref\System.Text.Encoding.Extensions.dll' and 'C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades\System.Text.Encoding.Extensions.dll'. Remove one of the duplicate referencesCuirass
@TaylorBuchanan your comment saved my day.Pasley
Z
6

It's good to hear that for many people updates to Visual Studio resolved their issues; however, it is worth pointing out that it is no longer recommended to try to consume .NET Standard 2.0 libraries from .NET Framework 4.6.1 projects because of bugs and other issues. You should use 4.7.2 or newer instead, where possible.

Immo Landwerth, Program Manager on the .NET team at Microsoft, tweeted:

Sorry but we messed up. We tried to make .NET Framework 4.6.1 retroactively implement .NET Standard 2.0. This was a mistake as we don't have a time machine and there is a tail of bugs.

If you want to consume .NET Standard 1.5+ from .NET Framework, I recommend to be on 4.7.2.

The .NET Standard comparability matrix has been updated with a footnote for 4.6.1 which reads:

The versions listed here represent the rules that NuGet uses to determine whether a given .NET Standard library is applicable. While NuGet considers .NET Framework 4.6.1 as supporting .NET Standard 1.5 through 2.0, there are several issues with consuming .NET Standard libraries that were built for those versions from .NET Framework 4.6.1 projects. For .NET Framework projects that need to use such libraries, we recommend that you upgrade the project to target .NET Framework 4.7.2 or higher.

Zohar answered 14/7, 2019 at 2:6 Comment(0)
D
2

Just in case some persons still having this issue like me

update your visual studio 2017 version to >15.3 (check version in help->about from the top menu), i had version 15.2 and seeing this error, i did everything i found over here or github but didnt fixed my issue. Then updated visual studio version and it fixed.

Dahl answered 12/10, 2017 at 5:17 Comment(1)
I have this problem with 2017 15.9Susceptible
W
2

I'm using Visual Studio 2017 with a C# UWP app. I had this error and a lot of my classes gave this error in my project. I fixed it by right-clicking on the References folder of your project in Solution Explorer and clicking "Add reference". Then I chose the "Browse" button at the bottom of the popup.

It takes you to File Explorer, and you will find the right file here:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll

or from the .NET Core 2.0 SDK you can find it

C:\Program Files\dotnet\sdk\2.0.0\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll

Once I added it, all my errors went away.

Waterfall answered 28/12, 2018 at 21:40 Comment(0)
V
0

If you are using MSBuild for CI like Jenkins, then update the build tools (you can download it again from https://www.visualstudio.com/downloads/#build-tools-for-visual-studio-2017) and make sure ".NET Core build tools" is checked.

Reference: https://github.com/dotnet/standard/issues/458

Ventura answered 30/11, 2017 at 19:36 Comment(0)
R
0

I had this issue in my test project when using TestServer to test my MVC views.

The test project, web project and associated libraries were all netcoreapp2.0.

Upgrading vs2017 to 15.5 did not work, and editing my test .csproj with the following suggested fixes did not work:

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

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

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

As per https://github.com/aspnet/Razor/issues/1212#issuecomment-297885722 however, this worked:

  <Target Name="CopyDepsFiles" AfterTargets="Build" Condition="'$(TargetFramework)'!=''">
    <ItemGroup>
      <DepsFilePaths Include="$([System.IO.Path]::ChangeExtension('%(_ResolvedProjectReferencePaths.FullPath)', '.deps.json'))" />
    </ItemGroup>    
    <Copy SourceFiles="%(DepsFilePaths.FullPath)" DestinationFolder="$(OutputPath)" Condition="Exists('%(DepsFilePaths.FullPath)')" />
  </Target>
Romp answered 7/12, 2017 at 3:30 Comment(0)
G
0

I had this exception myself when physically moving a project into a sub folder, causing the NuGet package references (path hints) being broken. After fixing them in the csproj file, everything was back to normal again.

Geography answered 25/2, 2019 at 10:29 Comment(0)
D
0

Another scenario which gives the same error message is if your web.release.config file changes the version of the framework that you're targeting. I don't know how our project ended up in this state, but we had a transform that changed us from targeting 4.7.2 to just 4.7

<compilation xdt:Transform="Replace" targetFramework="4.7" batch="true"
                 enablePrefetchOptimization="true" optimizeCompilations="true" />

Removing the targetFramework attribute (so it kept the same one as our web.config was using) fixed this error

Devoted answered 23/5, 2020 at 21:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.