Use Newtonsoft library in NetStandard 2.0 class library
Asked Answered
C

2

8

I am developing a class library based on the NetStandard 2.0 framework for multiple platform compatibility sakes, and I need to serialize and deserialize objects. So I added a reference to the Newtonsoft library.

The problem is that I have the following exception at runtime:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.'

I tried to manually add a reference to the System.ComponentModel.Annotations version 4.2.0.0 but this version is not available.

Is there a way to use Newtonsoft with NetStandard 2.0, or an alternative to perform serialization/deserialization operations?

Update: it seems that adding a reference to System.ComponentModel.Annotations" Version="4.4.1" and rebuilding the solution fixed the problem.

Here is the content of my csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
      <PackageReference Include="System.ComponentModel.Annotations" Version="4.4.1" />
    </ItemGroup>
</Project>
Caitlyncaitrin answered 13/12, 2017 at 1:36 Comment(6)
Does it give you a stack trace by any chance?Jurel
How did you add the reference? via nuget?Schnorr
Which package specifically are you referencing? Newtonsoft.Json? Newtonsoft.Json.Schema?Jurel
Please edit your question and include the content of your .csproj fileOculomotor
I am adding the reference via Nugget. I edited the question to explain how I solved the issue, thanks for your prompt replies. I am specifically referencing Newtonsoft.Json v10.0.3Caitlyncaitrin
You can answer your own questions, which is MUCH better than editing your question to include the answer.Medication
B
4

So I have been looking at referencing Newtonsoft.Json from the .NETStandard 2.0. It's all there and ready in version Newtonsoft.Json.11.0.2.

~/packages/Newtonsoft.Json.11.0.2/

enter image description here

Just reference it in csproj like so...

<Reference Include="Newtonsoft.Json">
  <HintPath>..\APAS.WebInterface\packages\Newtonsoft.Json.11.0.2\lib\netstandard2.0\Newtonsoft.Json.dll</HintPath>
</Reference>
Botswana answered 20/4, 2018 at 8:54 Comment(0)
S
1

The solution of @user9200027 to add a reference didn't work for me. However referencing as content does work, but it has the side effect of showing up in the solution explorer file list.

But note that if targeting multiple frameworks one should add a condition for the .net standard framework, otherwise it will override the file for the non .net standard frameworks as well.

Here is a sample .csproj entry:

<Content Condition="$(TargetFramework)=='netstandard2.0'"
    Include="$(NuGetPackageRoot)\newtonsoft.json\12.0.2\lib\netstandard2.0\Newtonsoft.Json.dll">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
  <Visible>False</Visible>
</Content>
Semitropical answered 16/7, 2019 at 0:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.