You must add a reference to assembly 'netstandard, Version=2.0.0.0

176,144

Solution 1

I think the solution might be this issue on GitHub:

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" />

I realise you're using 4.6.1 but the choice of .NET 4.7.1 is significant as older Framework versions are not fully compatible with .NET Standard 2.0.

I know this from painful experience, when I introduced .NET Standard libraries I had a lot of issues with NUGET packages and references breaking. The other change you need to consider is upgrading to PackageReferences instead of package.config files.

See this guide and you might also want a tool to help the upgrade. It does require a late VS 15.7 version though.

Solution 2

Manually editing the .csproj file and adding the reference below worked for me.

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

Thank you to Fahad Alshaya who suggested it here.

Solution 3

I had to do a combination of other people's answers on this thread.

  1. Install the NetStandard.Library via NuGet
  2. Manually editing the .csproj file and adding the reference. <Reference Include="netstandard" />
  3. Expanding project-->References in the VS Solution Explorer, right clicking on 'netstandard' and showing the properties page and setting "Copy Local" to true.

Solution 4

Deleting Bin and Obj folders worked for me.

Solution 5

After upgrading from 4.6.1 framework to 4.7.2 we started getting this error:

"The type 'System.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'." and ultimately the solution was to add the "netstandard" assembly reference mentioned above:

<compilation debug="true" targetFramework="4.7.1" >
    <assemblies>
      <add assembly="netstandard, Version=2.0.0.0, Culture=neutral, 
            PublicKeyToken=cc7b13ffcd2ddd51"/>
    </assemblies>
  </compilation>
Share:
176,144
empz
Author by

empz

Updated on May 09, 2022

Comments

  • empz
    empz about 2 years

    The project is an ASP.NET MVC Web App targeting the .NET Framework 4.6.1.

    All of a sudden (some NuGet packages were upgraded) I started to get the following error during runtime:

    CS0012: The type 'System.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'.

    In my main view Index.cshtml, in a line where I make use of @Html.ActionLink

    I do have .NET Core SDK 2.0 and .NET Framework 4.7.1 installed in my machine but I don't want to include a reference to it. This is a .NET Framework web app only, it's hosted on Windows IIS where the installed framework is 4.6.1, there's no NET Core installed in the server.

    So why is it asking to add a reference to netstandard? How can I fix it without referencing netstandard but the full Windows .NET Framework 4.6.1?

    I've checked out a previous commit which worked fine and I'm still getting this error. So it's not related to NuGet packages being upgraded. Seems to be something on my local dev machine.

    If a publish the app to a directory and run it with IIS it works.

    Link to .csproj gist

    packages.config

    <?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="Antlr" version="3.5.0.2" targetFramework="net46" />
      <package id="BundleTransformer.Core" version="1.9.69" targetFramework="net46" />
      <package id="BundleTransformer.Handlebars" version="1.9.73" targetFramework="net46" />
      <package id="BundleTransformer.Less" version="1.9.69" targetFramework="net46" />
      <package id="BundleTransformer.Yui" version="1.9.52" targetFramework="net46" />
      <package id="Dapper" version="1.42" targetFramework="net46" />
      <package id="EcmaScript.Net" version="1.0.1.0" targetFramework="net46" />
      <package id="EntityFramework" version="6.0.0" targetFramework="net46" />
      <package id="Glimpse" version="1.8.6" targetFramework="net46" />
      <package id="Glimpse.AspNet" version="1.9.2" targetFramework="net46" />
      <package id="Glimpse.Mvc5" version="1.5.3" targetFramework="net46" />
      <package id="JavaScriptEngineSwitcher.Core" version="1.2.4" targetFramework="net46" />
      <package id="JavaScriptEngineSwitcher.V8" version="1.3.0" targetFramework="net46" />
      <package id="jQuery" version="2.1.3" targetFramework="net46" />
      <package id="jQuery.Validation" version="1.13.1" targetFramework="net46" />
      <package id="LowercaseDashedRoute" version="1.0.14" targetFramework="net46" />
      <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net46" />
      <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net46" />
      <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net46" />
      <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net46" />
      <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net46" />
      <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net46" />
      <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net46" />
      <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net46" />
      <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net46" />
      <package id="Microsoft.Owin" version="3.0.1" targetFramework="net46" />
      <package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net46" />
      <package id="Microsoft.SqlServer.Compact" version="4.0.8876.1" targetFramework="net46" />
      <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net46" />
      <package id="Moment.js" version="2.10.2" targetFramework="net46" />
      <package id="Newtonsoft.Json" version="10.0.3" targetFramework="net46" />
      <package id="NWebsec" version="5.1.1" targetFramework="net46" />
      <package id="NWebsec.Core" version="2.1.0" targetFramework="net46" />
      <package id="NWebsec.Mvc" version="5.1.1" targetFramework="net46" />
      <package id="Owin" version="1.0" targetFramework="net46" />
      <package id="Respond" version="1.4.2" targetFramework="net46" />
      <package id="Sendgrid" version="6.3.0" targetFramework="net46" />
      <package id="SendGrid.SmtpApi" version="1.3.1" targetFramework="net46" />
      <package id="Serilog" version="2.6.0" targetFramework="net46" />
      <package id="Serilog.Enrichers.Environment" version="2.1.2" targetFramework="net46" />
      <package id="Serilog.Sinks.File" version="3.2.0" targetFramework="net46" />
      <package id="Serilog.Sinks.RollingFile" version="3.3.0" targetFramework="net46" />
      <package id="Serilog.Sinks.Sentry" version="2.1.4" targetFramework="net46" />
      <package id="SerilogWeb.Classic" version="2.1.17" targetFramework="net46" />
      <package id="SharpRaven" version="2.2.0" targetFramework="net46" />
      <package id="System.Data.SQLite" version="1.0.108.0" targetFramework="net46" />
      <package id="System.Data.SQLite.Core" version="1.0.108.0" targetFramework="net46" />
      <package id="System.Data.SQLite.EF6" version="1.0.108.0" targetFramework="net46" />
      <package id="System.Data.SQLite.Linq" version="1.0.108.0" targetFramework="net46" />
      <package id="Twitter.Bootstrap.Less" version="3.3.4" targetFramework="net46" />
      <package id="WebActivatorEx" version="2.0.6" targetFramework="net46" />
      <package id="WebGrease" version="1.6.0" targetFramework="net46" />
      <package id="YUICompressor.NET" version="2.7.0.0" targetFramework="net46" />
    </packages>
    

    When publishing the app using VS2017 to the file system I get the following warning in the output:

    The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure: netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51. The dependencies are: System.Transactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089. You should either ensure that the dependent assembly is correct for the target framework, or ensure that the target framework you are addressing is that of the dependent assembly.

    But I can't find anything on System.Transactions. How can I search what's referencing it?

    • Ricardo Peres
      Ricardo Peres about 6 years
      What NuGet packages are you using? Are you sure you are not referencing an ASP.NET Core one?
    • empz
      empz about 6 years
      @RicardoPeres VS2017 doesn't let you add a package targeting a framework different that the one the project is targetting. I've uninstalled and reinstalled all packages but the error is the same. I don't understand why it happens and even why during runtime...
    • McGuireV10
      McGuireV10 about 6 years
      The System.Data.SQLite.Core package is based upon .NET Core (or .NET Standard, I didn't go looking for the source or docs). Look at the error message at the very end of your project file.
    • empz
      empz about 6 years
      Why do you said is based upn .NET Core? I'm looking at the packages\System.Data.SQLite.Core.1.0.108.0\lib directory and it contains a folder for each version of the .NET Framework, including 4.6 which is what I'm using. I don't think that's the issue here.
    • empz
      empz about 6 years
      Also, removing the package didn't solve the issue.
    • Simon Mourier
      Simon Mourier about 6 years
      When I face unsolvable issues like this, what I do is 1) carefully note what are the project's reference (or save the .csproj somewhere), 2) remove packages.config file, remove all external references (alternatively you can write a new project from scratch only with the original static files, .cs, etc.), 3) configure VS Nuget to use Package Reference instead of legagy packages.config mode docs.microsoft.com/en-us/nuget/reference/… and 4) add all needed references back. Most of the time, it works and I have no clue why it failed before.
    • Andrei U
      Andrei U about 6 years
      Have you recently updated some nuget package references? that might cause you some grievance
    • b k
      b k almost 4 years
      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!!
    • inifus
      inifus almost 3 years
      I've tried all of these solutions, even the ones from the other answers. nothing works for me. it builds in visual studio, but jenkins that use msbuild v19, gets this error. do I need to upgrade msbuild? I upgraded my application to net framework 4.7.2 in hopes to solve this, but still the same thing. any ideas?
  • Igor
    Igor about 6 years
    ok hopefully we almost there."I added the reference to NETStandard.Library instead of NETStandard.Library.NETFramework" check this post: github.com/dotnet/standard/issues/391
  • Igor
    Igor about 6 years
    Usually System.Transactions is not part of your references, try to add it in your references by: right click on your references if you are using Web Application or in your Project if you are using Web Site. find the System.Transactions in the .NET references. You should find the System.Transactions in your references.
  • empz
    empz about 6 years
    Tried all, no luck. This is not a .NET Core project as I've said. This is a normal .NET Framework 4.6.1.
  • Quango
    Quango over 5 years
    I think anyone who arrives here should also look at this excellent guidance on writing open source libs by the author of Newtonsoft.JSON: docs.microsoft.com/en-us/dotnet/standard/library-guidance/… - in particular "Avoid including a netstandard1.x" target, and use .NET 4.7.2 as the lowest real version that supports .netstandard 2.0
  • Kushal Shah
    Kushal Shah over 5 years
    Exactly what was needed when upgraded to v4.7.2 (non core app) and got an exception like the following: "The type 'DateTime' 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'.
  • petrosmm
    petrosmm over 5 years
    Does this also have to added to the references in the csproj file, or it's only needed in the web.config?
  • Piotr Kula
    Piotr Kula about 5 years
    Great answer. Thanks. I was in process of upgrading an old utilities package and slapped a new standard 2.0 in its place. This web app in particular had issues because the <add assembly="netstandard was missing and also httpRuntime was not 4.7.1- This is the answer! Thanks !!
  • Ulysses Alves
    Ulysses Alves almost 5 years
    Where should this be put inside the project file? Inside which other "tag"?
  • kamilk
    kamilk almost 5 years
    @UlyssesAlves Somewhere next to other Reference tags
  • Ulysses Alves
    Ulysses Alves almost 5 years
    @kamilk "somewhere next to some other" is not very accurate. Well, I looked at some other projects, but unfortunately this didn't work for me and I've decided to attack this problem from another perspective, which doesn't involve changing the .net version of the project.
  • David Acero
    David Acero over 4 years
    This one worked for me, I also had to install the NetStandard.Library via NuGet.
  • Benj Sanders
    Benj Sanders over 4 years
    The little things that "get you"... Great answer.
  • FrenkyB
    FrenkyB over 4 years
    Setting 'Copy local' did it for me.
  • Triynko
    Triynko about 4 years
    That didn't work for me, but adding <add assembly="netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"/> under the main web.config file's system.web/compilation/assemblies section did. I kept switching back and forth from one solution to the other, and the Reference didn't work, but the web.config setting did. I was targetting .NET 4.7.2 and MVC 5.2.7. This is a NASTY bug.
  • Triynko
    Triynko about 4 years
    Also, the web.config file in the Views folder is never automatically updated, so you have to manually set the correct MVC DLL version there (mine was at 5.2.2 and I had to manually change it to to 5.2.7 after my upgrade).
  • Triynko
    Triynko about 4 years
    This works. Another thing that works (without this change) is to add the <Reference Include="netstandard" /> to the csproj file, BUT... that only works if you ALSO set CopyLocal to true. None of this makes any sense.
  • Triynko
    Triynko about 4 years
    Where is this documented? How would anyone every guess to put this line in a web.config file? Incidentlly, what works equally well is adding a line to the csproj that references "netstandard", and setting CopyLocal to true. Even so, that's not even listed as a possible reference in the references dialog anywhere, so the only way to get it is to add it manually. This is insane. And I'm targeting .NET 4.7.2, which is supposed to be fully compatible with .NET standard, yet I have to do these obscure steps to get it work.
  • DarkHawk
    DarkHawk about 4 years
    This solution works perfectly, plus the project does not depend on compiling in debug mode.
  • ShaneH
    ShaneH about 4 years
    In our case we had had several standard 2.0 assemblies in use by a 4.7.2 ASP.Net project but we added some JsonConverter from the System.Text.Json.Serialization namespace to one of the standard 2.0 projects and that caused this error. This solution resolved our issue.
  • Reed Shilts
    Reed Shilts almost 4 years
    Close VS, delete bin, delete .vs - i'm happy.
  • bairog
    bairog over 3 years
    The same for me: .NET Framework 4.8 application that referencing NetStandard 2.0 library via PackageReference (latest Visual Studio 2019 16.7.7).
  • costaparas
    costaparas about 3 years
    Please check out the formatting help page to improve your formatting.
  • ekarankow
    ekarankow about 3 years
    Setting 'Copy local' did it for me.
  • Andrew Gale
    Andrew Gale about 3 years
    I kept target 4.6.2 and it still fixed it. thanks.
  • Jahmic
    Jahmic over 2 years
    Complete snippet to add to csproj file: <ItemGroup> <Reference Include="netstandard" /> </ItemGroup>
  • Nicolas
    Nicolas over 2 years
    awesome! this pointed me to the right direction: only framework 4.7.2 fully supports netstandard2.0 without any problems: weblog.west-wind.com/posts/2019/Feb/19/… (I wished Microsoft had explained this)
  • MaliEbbae
    MaliEbbae over 2 years
    This also works for downgrading from 4.6.1 to 4.5.
  • Patee Gutee
    Patee Gutee over 2 years
    This works for me! I wonder why would users need to resort to finding out the hard way and making unnecessary manual config changes instead of Microsoft doing something about this bug with .NET Standard?
  • corix010
    corix010 over 2 years
    Steps 2 and 3 helped me get rid of errors in my app.
  • Garr Godfrey
    Garr Godfrey over 2 years
    it worked but this bothers me no end. building for 4.7.2 and referencing some .netstandard assemblies, but 4.7.2 is supposed to be fully .netstandard 2.0 compliant
  • curious.netter
    curious.netter over 2 years
    WebConfig entry did not helped me, it was even emitting warning that assemblies is illegal child under system.web. Installing netstandard nuget and entry in the .csproj file helped me in my 4.7.2 project. Remember to set netstandard reference to copy local from properties.
  • Vedran Mandić
    Vedran Mandić about 2 years
    this should be the accepted answer... just woah, works, thanks!