Legends
Legends

Reputation: 22672

Newtonsoft.JSON v9.01 + FileNotFoundException (.NET Core Class library)

(VS2015 Update 3 + Patch)

I have a plain .NET console application (.NET 4.6) and reference a .NET core class library that targets NetStandard v1.3. The class library has a reference to Newtonsoft.JSON.

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Newtonsoft.Json": "9.0.1"
  },
  "buildOptions": { "platform": "anycpu" },

  "frameworks": {
    "netstandard1.3": {
      "imports": "dnxcore50"
    }
  }
}

The referenced NewtonSoft.JSON package is deployed here:

C:\Users\UserAccount\.nuget\packages\Newtonsoft.Json\9.0.1

The Exception:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in DotNetConsoleApplication.exe

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

I guess the .net core lib would reference the dll from the netstandard1.0 folder.

enter image description here

Upvotes: 13

Views: 5502

Answers (5)

Legends
Legends

Reputation: 22672

Solved 31.07.2016

Create a fresh plain .NET console app (not .NET Core) and a .NET Core class library, without doing any referencing between them upfront.

Scenario:
1. Console app based on .NET 4.6, which references a
2. .Net Core Classlibrary (has a reference to Newtonsoft.JSON v9.01)

The .NET core class library is configured as follows (project.json):

{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.0",
    "Newtonsoft.Json": "9.0.1"
  },
  "buildOptions": { "platform": "anycpu" },

  "frameworks": {
    "netstandard1.3": {
      "imports": "dnxcore50"
    }
  }
}

Solution

1.) Create a Nuget package from the .Net core class library project first.

Open the command line as admin. Go (cd) to the project folder of the .NET core class library project (.xproj).

Run the following command:

  dotnet pack

enter image description here

The "pack" parameter will create a nuget package out of the .NET Core class library and copy the package to the debug/release folder, depends on your project configuration.

Copy the nuget package files to a folder where you host your local nuget packages. I have copied them to:

C:\Users\Admin.nuget\packages\LocalPackages\NetCore46ClassLibrary


Screenshot: enter image description here
2.) If you don't have a local Nuget feed,you have to create one first!

The local Nuget folder (I named it "LocalPackages") will host your custom Nuget packages. The local Nuget Feed will point to "LocalPackages", which is the root folder for all local packages.

enter image description here

After you have created the local nuget feed and copied the nuget package of your .net core class library somewhere beneath the localPackages folder, you are ready to install your .net core class library nuget package.

3.) Install the .NET Core library Nuget Package into the .NET console app

you now have to open the Package Manager Console again. Choose Package Source: Local Packages (This is my local feed name, may be different). And the default project should be your .NET console app.

enter image description here

Install your .net core class library nuget package into the console app, in my case:

install-package NetCore46ClassLibrary

That's it !

My system:
enter image description here

dotnet --version
1.0.0-preview2-003121

Upvotes: 2

M.Hassan
M.Hassan

Reputation: 11032

As a workaround solution, In visual studio 2017, you can modify the the NetStandard project .csproj as multi-target:

           <TargetFrameworks>netstandard1.3;net461</TargetFrameworks>

Rebuild the solution and the referenced dll (NewtonSoft.JSON) will be copied to bin folder of the console project.

Have a look to my implementation in :Workaround Solution

Upvotes: 0

Sourcerer
Sourcerer

Reputation: 1906

Creation of NuGet package is a solution but not the easiest one.

Microsoft finally admitted this is a problem and will fix it, expectantly, in NuGet version 4.0.1, the first update to NuGet 4 after VS 2017 ships.

The cleanest workaround now is to add <RestoreProjectStyle>PackageReference</RestoreProjectStyle> to a legacy project. However according to Rob Relyea MS will ignore this property after RTM so another workaround is <PackageReference Update="PlaceholderToConvinceThisProjectToGetTransitivePackageReferenceFromProjectReferences"/>.

Upvotes: 4

Errbots
Errbots

Reputation: 11

I had the same error recently, after including Newtonsoft.Json 6.0.8 in a dotnet core console app. The solution was to include the System.Runtime.Serialization.Primitives dependancy to the project.json config.

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {
    "System.Runtime.Serialization.Primitives":  "4.0.10-*",
    "Newtonsoft.Json": "6.0.8"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

Upvotes: 1

leetibbett
leetibbett

Reputation: 843

Could be that the Newtonsoft assembly is 64 bit and your .Net Core project is 32 bit. Also could be that you have multiple versions of Newtonsoft referenced.

Upvotes: 1

Related Questions