Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

You must add a reference to assembly netstandard errors #542

Closed
Petermarcu opened this issue Oct 19, 2017 · 91 comments
Closed

You must add a reference to assembly netstandard errors #542

Petermarcu opened this issue Oct 19, 2017 · 91 comments
Milestone

Comments

@Petermarcu
Copy link
Member

@fahadabdulaziz opened this here: microsoft/dotnet#527

Hi,

After targeting .Net Framework 4.7.1, in many projects referencing net standard libraries. I get these errors:
errs

csharp 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'

Though I all the projects including that targeting net471 are building successfully!

Any thoughts ?

@Petermarcu
Copy link
Member Author

@AlexGhiondea @terrajobst

@juscelior
Copy link

Try add netstandard reference in web.config like 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" />

solution-01

@fahadabdulaziz
Copy link

Thank you,
But I'm using WinForms, which mean I don't have web.config.

@fahadabdulaziz
Copy link

I managed to fix this problem by adding to csproj file with the references:

<Reference Include="netstandard" />

I don't know if this workaround is good or has side effects, but it silenced the errors for me.

@mbp
Copy link

mbp commented Oct 30, 2017

Got the same problem, and the workaround fixes it. But why should this be necessary? Isn't 4.7.1 supposed to implement .NET Standard 2.0?

@fahadabdulaziz
Copy link

Just a question, do you have ReSharper installed ?

@mbp
Copy link

mbp commented Oct 31, 2017

No

@nandithakw
Copy link

I had the same issue with Visual Studio 15.4.4 with net471. Project didn't give errors with net47.
Adding
<ItemGroup> <Reference Include="netstandard" /> </ItemGroup>
as fahadabdulaziz suggested resolves the issue.

@critesjm
Copy link

Ran into a similar problem in a project targeting framework 4.6.1 but using newer netstandard memory cache libraries - fixed it with <Reference Include="netstandard" /> but confused about why we need that and/or it's not handled automatically by related tooling.

@danmoseley
Copy link
Member

@AlexGhiondea any idea why they are having to add the netstandard reference manually on 471?

@jp2masa
Copy link
Member

jp2masa commented Dec 5, 2017

It was fixed for Visual Studio 15.5: dotnet/msbuild#2567.

@joperezr
Copy link
Member

joperezr commented Dec 5, 2017

@AlexGhiondea any idea why they are having to add the netstandard reference manually on 471?

This is because in 4.7.1 we no longer have to use the Support package to implicitly add references to facades since netstandard comes inbox starting from 4.7.1. That said, as @jp2masa we had a bug that was fixed in VS15.5 so you no longer have to add this manually.

@plioi
Copy link

plioi commented Dec 19, 2017

@joperezr I have VS 15.5.2, and experience the same errors when building from the command line. Visual Studio says there are no errors, but I get these errors from dotnet build.

My problem was that my SDK was pinned to 2.0.3 via global.json, and resolved when pinning to 2.1.2. Strange that VS must be ignoring global.json.

@joperezr
Copy link
Member

yeah I believe that what is happening is that VS doesn't use the CLI(SDK) for building, but instead calls into MSBuild directly, which version is tied to the VS version. That explains why you only see this when trying to build using the CLI, but I'm glad that the global.json workaround fixed the issue.

@critesjm
Copy link

yeah it seems to be just when you're using the msbuild cli tool to run builds that this issue crops up. unfortunately it seems to have caused some problems on our build server when i added the concrete reference to netstandard to our project so i had to remove it and i'm back in the same boat i was before when i try to cli build from VS Code :(

@Nefcanto
Copy link

Nefcanto commented Jan 4, 2018

These suggestions didn't solve my problem. I was working with a project and everything was working fine. I'm on .NET Core 2.0.3. All of a sudden it broke and now it gives me that annoying error. Any idea what might be wrong?

@joperezr
Copy link
Member

joperezr commented Jan 8, 2018

@Nefcanto can you try with the latest released CLI? That should automatically add the reference to the netstandard facade if it needs it.

@MovGP0
Copy link

MovGP0 commented Jan 11, 2018

I have the following issue with an integration test:

Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException : One or more compilation failures occurred:
lu0lulp4.aee(4,62): error CS0012: The type 'Attribute' 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'.
lu0lulp4.aee(4,81): error CS0518: Predefined type 'System.String' is not defined or imported
lu0lulp4.aee(4,110): error CS0518: Predefined type 'System.Type' is not defined or imported
lu0lulp4.aee(4,11): error CS0518: Predefined type 'System.Void' is not defined or imported
lu0lulp4.aee(8,11): error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
...

The .csproj file looks as following:

<Project ToolsVersion="15.0" Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="netstandard" />
  </ItemGroup>

<!-- NuGet Libraries -->
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.0.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor" Version="2.0.2" />
    <PackageReference Include="Microsoft.AspNetCore.TestHost" Version="2.0.1" />
    <PackageReference Include="Microsoft.CodeCoverage" Version="1.0.3" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
    <PackageReference Include="NodaTime.Testing" Version="2.2.3" />
    <PackageReference Include="NSubstitute" Version="3.1.0" />
    <PackageReference Include="NUnit" Version="3.9.0" />
    <PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
    <PackageReference Include="Roslynator.Analyzers" Version="1.6.0" />
    <PackageReference Include="Sendgrid" Version="9.9.0" />
  </ItemGroup>

<!-- reference to project under test -->
  <ItemGroup>
    <ProjectReference Include="..\Portal\Portal.csproj" />
  </ItemGroup>

<!-- Unit Test GUID -->
  <ItemGroup>
    <Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
  </ItemGroup>

<!-- include Razor Views -->
  <ItemGroup>
    <Folder Include="Views\" />
    <Content Include="..\Portal\Views\**\*">
      <Link>Views\%(RecursiveDir)\%(FileName)%(Extension)</Link>
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
</Project>

Visual Studio Version 15.5.3.

Compilation of the project works fine and there are no assembly version conflicts reported during build.

@Justodium
Copy link

Same or similar problem
What I have done:

  1. having an existing net462 project
  2. created a netcore WebApplication with Angular from the Templates of VS2017 15.5.2
  3. changed in the webapplication.csproj the TargetFrameworks to <TargetFrameworks>netstandard2.0;net462</TargetFrameworks>
  4. added all aspnetcore references separately since "Microsoft.AspNetCore.All" isn't supported
  5. referenced the WebApplication from the net462 project
  6. started the project with a simple response from Startup
    app.Run(async (context) => { await context.Response.WriteAsync("Hello World!"); });
    Everything is working fine and i got the response in the browser.

But:
By using mvc (default route) I got this error:

One or more compilation references are missing. Ensure that your project is referencing 'Microsoft.NET.Sdk.Web' and the 'PreserveCompilationContext' property is not set to false.

The type 'Attribute' 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'.

I have tried the ideas and solutions from #2981, #860 and this one, but still the same error.

@joperezr
Copy link
Member

cc: @dsplaisted

@loop-evgeny
Copy link

In my case this was caused by the System.Immutable.Collections package and the workaround was to downgrade it: #534

@MattJeanes
Copy link

For me, the error was on a VSTS build agent - the solution was to install the latest dotnet SDK 2.1.4 using the .NET Core Tool Installer task

@MatthewSteeples
Copy link

@MattJeanes
Copy link

@MatthewSteeples Those are the hosted agents, I was using a private one (should have clarified, my bad) - but yes that should solve the issue if anyone is getting it using the hosted agents.

@jernelson7
Copy link

None of the above suggestions helped me. I'm using VS15.6 and .NET Core SDK 2.1.4. I don't have a global.json file. I explicitly added a reference to netstandard in each of the netstandard .csprojs. Any other ideas?

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

@joperezr
Copy link
Member

@jernelson7 this reference isn't required for the netstandard projects. If you don't have the up-to-date tools, you just need to add it to the project consuming those netstandard projects, so in this case, most likely you need to add to it your app directly. Check the error to see which project is complaining about this.

@wstaelens
Copy link

only the comment of juscelior commented on Oct 25 2017 helped!

I los a few hours on this as all I got was this:

Could not load file or assembly 'Microsoft.CodeAnalysis, Version=2.7.0.0,

@watfordgnf
Copy link

@joperezr so what was the fix for this? And can you pin the fix someplace where people can find it. There are a bunch of random ideas that fix random problems for random people, but nobody from MS has put down The Fix(tm) that actually resolves this issue.

We're blocked on upgrading a .NET 4.6.2 ASP.Net Web Forms project from going to .NET 4.7.2 so we can start sharing authentication cookies with its ASP.Net Core sister site. Bizarrely enough, publishing+precompiling in Debug mode allows this to work, but the moment you disable debug mode for precompilation you get jerked around by missing references to 'netstandard'.

@joperezr
Copy link
Member

There are a bunch of random ideas that fix random problems for random people, but nobody from MS has put down The Fix(tm) that actually resolves this issue.

Getting the error message on your project is a symptom which may be caused by many different things like for example: Not enabling auto generation of binding redirects, using legacy VS versions to compile, using old-style packages.config instead of PackageReference for your NuGet references, manually adding references to dlls inside a restored NuGet package, etc. That is kind of the reason why you have seen different fixes work for different people, because unfortunately this problem may arise due to different circumstances. That said, the general advise which has worked most of the time is:

  • If you can re-target to 4.7.2 or higher, this problem should go away
  • If not, make sure that you:
  • Enable auto-generation of binding redirects. Link
  • Migrate your project from packages.config into PackageReference. Link
  • Make sure you are running on the latest version of Visual Studio. This is because we have added a lot of tooling during the build in order to ensure this problem won't happen.

We're blocked on upgrading a .NET 4.6.2 ASP.Net Web Forms project from going to .NET 4.7.2 so we can start sharing authentication cookies with its ASP.Net Core sister site.

This really shouldn't block you at all, since netstandard.dll is inbox on .NET 4.7.2, as well as all the support for running netstandard assemblies. If you are hitting these sorts of issues while re-targeting, it may be because your project file has not been re-targeted correctly and perhaps has added some properties/references that were only meant for .NET 4.6.2. Whenever I need to re-target a .NET Framework project, I usually instead create a brand new 4.7.2 project, and just copy the source files over as well as add the missing references

@watfordgnf
Copy link

  1. This is targeting 4.7.2 (as stated)
  2. <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>, <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
  3. Does ASP.Net Web Forms support PackageReference? I was under the impression it does not.

@barakakha
Copy link

In my case i found out that the problematic nuget was System.Memory in version 4.5.1. when updated to version 4.5.3 it works!!

@TPSDrummer
Copy link

I get this when I call:

var rc = new RestClient(clientID, clientSecret,true);

InnerException | {"Could not load file or assembly 'Flurl.Http, Version=2.3.2.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.":"Flurl.Http, Version=2.3.2.0, Culture=neutral, PublicKeyToken=null"} | System.Exception {System.IO.FileNotFoundException}

This is from a clean install of the RingCentral.Client and the default dependencies install.

@mafshin
Copy link

mafshin commented Apr 12, 2021

The issue for me was having a NETStandard 2.1 library referenced and then removed but still present in binary directory. Due to dynamic loading (Autofac) it was trying to load it and complained about. So beside cleaning the project also ensure you delete everything in bin folder directly.

@farhadnowzari
Copy link

farhadnowzari commented Jun 17, 2021

Hi,
we have the same issue with consuming dotnet standard 2.0.0 in dotnet framework 4.7.2, adding the assembly line fixes the issue but why that is NOT there by default and it is documented nowhere.

This OPEN for 4 years!!!! no one has considered to fix it?

@lastlink
Copy link

  • for those doing a publish single file and trim. none of these solutions worked for me. Although these helped.
    • e.g. dotnet publish -c Release -r win-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true
    • single exe wouldn't work with -p:PublishTrimmed=true until I added this in the csproj
<ItemGroup>
   <TrimmerRootAssembly Include="netstandard" />
</ItemGroup>

@839998664
Copy link

I am running msbuild on a .net framework application in a Jenkins Build Job and getting the following error

error CS0012: The type 'Action<>' 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'

... No option to change any code... Is there a workaround other than that...

@ztatw
Copy link

ztatw commented Dec 20, 2021

I haven't got this error in Rider IDE, only CLI.

I got same error on some versions of MSBuild, e.g. the latest MSBuild 16(16.11.2) works fine, the MSBuild 17(17.0.4?) throws this error, I still got this error on some versions of MSBuild 16, seems randomly

@rominator1983
Copy link

I have pinned one of those errors (The type 'System.Enum' is defined in an assembly that is not referenced. You must add a reference to assembly 'netstandard, Version=2.0.0.0,) on our side on the following situation:

  • GenerateSerializationAssemblies set to On in csproj. Turning to Off leads to a runtime error of course
  • csproj has an old Web Service reference and therefor needs the XML serialization types/assemblies
  • targetframework is net48
  • One of the classes of the web reference contains a partial class with a property (XmlIgnore attribute of course but this does not seem to help)
  • The aforementioned property is of an Enum type. And this enum type is defined in another class library which has (you guessed it) target framework netstandard2.0

So my solution was to remove the partial class altogether and handle whatever was done there somewhere else.

@Soggi
Copy link

Soggi commented Apr 18, 2022

I had this same issue when targeting 4.8.0,

Adding

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

Fixed it for me.

@terrajobst
Copy link
Member

We believe the issue has been fixed in MSBuild. If the issue still exists, file an issue in dotnet/msbuild.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests