0

I have a .NET Standard class library project that's being packaged into a nuget package. It has a dependency on Newtonsoft.Json.

I built the nuget package by checking the "Generate nuget package on build" on the project properties, under the package tab.

I also tried doing nuget pack. Here's my nuspec:

<?xml version="1.0" encoding="utf-8"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>$author$</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <license type="expression">MIT</license>
    <projectUrl>http://project_url_here_or_delete_this_line/</projectUrl>
    <iconUrl>http://icon_url_here_or_delete_this_line/</iconUrl>
    <description>$description$</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2020</copyright>
    <tags>Tag1 Tag2</tags>
    <dependencies>
      <dependency id="Newtonsoft.Json" version="12.0.3" />
    </dependencies>
  </metadata>
</package>

Then I ran this:

nuget pack -Prop Configuration=Release -IncludeReferencedProjects

Then I have a .NET Framework 4.8 project and we added the .NET Standard nuget package to it. The problem is that I'm getting the following error:

FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

I needed to add the Newtonsoft.Json in nuget package manager manually to the .NET Framework project. It's not being installed when I install the .NET Standard nuget package.

How can I build a .NET Standard nuget package that will resolve its dependencies automatically?

Lance
  • 2,774
  • 4
  • 37
  • 57
  • for me, running `dotnet new classlib`, `dotnet add package newtonsoft.json`, `dotnet pack` creates a package which has a dependency on newtonsoft.json, and therefore when installed in another project brings it in as a transitive dependency. I'm really not sure what problem you're experiencing. – zivkan Aug 12 '20 at 14:17
  • And you're adding it to a .NET Framework project? The issue I'm dealing with does not happen in projects other than .NET Framework. – Lance Aug 12 '20 at 14:37
  • When you install your net standard nuget in your net48 main project, please add a binding redirect in `app.config` file: `` – Mr Qian Aug 13 '20 at 03:03
  • Please let us know if it helps or not. – Mr Qian Aug 13 '20 at 03:03

1 Answers1

0

nuget restore resolves dependencies of nuget packages automatically.

I assume that you reference a different version of Newtonsoft.Json in your .NET Framework 4.8 project.

enter image description here

Newtonsoft.Json is a strong-named assembly.
The runtime is picky regarding versions when it comes to strong-named assemblies.

https://learn.microsoft.com/en-us/dotnet/standard/library-guidance/strong-naming

The downside to strong naming is that the .NET Framework on Windows enables strict loading of assemblies once an assembly is strong named. A strong-named assembly reference must exactly match the version referenced by an assembly, forcing developers to configure binding redirects when using the assembly

Your .NET Framework 4.8 Project wants to use two different versions that is not possible with strong-named assemblies.
It would work if Newtonsoft.Json were not strongly named.

Adding a binding redirect like mentioned in the comments by @Perry Qian-MSFT should solve your problem.

This can be done by running the following command in the VS Package Manager Console
Get-Project -Name <ProjectName> | Add-BindingRedirect

You can also enable Auto-generate binding redirects in the project properties: enter image description here

stefan
  • 121
  • 1
  • 2
  • 6