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

(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.

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

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
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:
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.
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.
Install your .net core class library nuget package into the console app, in my case:
install-package NetCore46ClassLibrary
That's it !
My system:
dotnet --version
1.0.0-preview2-003121

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.

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

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

Related

Unable to restore a nuget of .net core class lib into another

I am trying to create a nuget for a class library which I want to be consumed across different applications. This class library is in .net core (4.5.1 framework).
The project.json looks like below:
{
"version": "1.0.0",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Microsoft.AspNetCore.Diagnostics.Abstractions": "1.0.0",
"Microsoft.AspNetCore.Http": "1.0.0",
"log4net": "2.0.8"
},
"description": "Logging from .net core",
"frameworks": {
"netstandard1.6": {
"imports": [ "dnxcore50" ]
}
}
}
I build my class library and create a nuget package and put it in my internal repository. I created nuget package using 2 different ways:
create a .nuspec file( command : nuget spec) and pack the .nuspec file (command nuget pack projectname.nuspec) -> results in a .nupkg file
Command : dotnet pack project.json -> results in .nupkg and .sysmbols.nupkg files
When I try to restore this package from my repo into another application, I get error :
for nuget created with method 1 :
Package TestLib 1.0.0 is not compatible with netstandard1.6 (.NETStandard,Version=v1.6). Package TestLib 1.0.0 supports: net (.NETFramework,Version=v0.0)
One or more packages are incompatible with .NETStandard,Version=v1.6.
When I looked into References tree in Solution explorer, I see that the error is on my referenced class library package with message :
NU1002 The dependency TestLib does not support framework .NetStandard, version =v1.6
For method 2:
Restore will be successful, but I do not get/use any classes from the library. Basically an empty dll reference.
The project.json of my application is simple and straight.
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.1"
},
"TestLib": "1.0.0"
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
I am going clue less why this behaviour!! Why I am not able to restore a .net core library nuget into another. I also tried restoring to a . net core console application, .net core web application all with same results.
I am using VS2015 ENterprise edition with .netCore tools Preview 2

How to create a chocolatey package for a .NET Core cli application?

I have a very basic cli application(that basically prints "hello world") written in C# and that uses the .net core runtime.
I tried to create a chocolatey package by:
running choco new hcli
modifing the generated .nuspec file manually to supply info(version, author...)
running choco pack
This produced a .nupkg file, when I run choco install hcli.0.0.1.nupkg I get ERROR: This package does not support 64 bit architecture.
I am suspecting that chocolatey does not support project.json based projects, the documentation does not mention anything about .net core.
What am I doing wrong?
project.json file:
{
"version": "0.1.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true,
"outputName": "hcli"
},
"dependencies": {},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
},
"runtimes": {
"win7-x64": {}
}
}
Chocolatey does not support visual studio projects nor project.json at the time of this post.
Fix the error
What you are seeing is a pretty common error if you have not set up or adjusted any of the packaging.
Have you reviewed the contents of tools\chocolateyInstall.ps1 after you generated the packaging? I would review and adjust those automation scripts that were generated (and review the readme).
If you don't need the automation scripts, simply remove them and have your binaries in the package.
As you have indicated, there is a much more drawn out article at https://chocolatey.org/docs/create-packages
Alternative Option - Use NuGet to pack
You can always look to use NuGet to generate the package and then consume it with Chocolatey. As long as it is compatible with NuGet v2 (currently), you should be good to go. The other aspect of that is that if you have dependencies at the DLL level, please include them in the packaging - dependencies are really at the application level. Like a dependency on dotnetcore package.

proper way to sign .net core assembly

I'm trying to sign a .net core lib, and I'm just not sure if I did everything correctly
1) using VS Command Promp I called sn -k mykey.snk
2) copied mykey.snk to myproject folder
3) in project.json added keyfile
"frameworks": {
"netstandard1.6": {}
},
"buildOptions": {
"outputName": "MyLib",
"keyFile": "mykey.snk"
}
is this correct, is the library (dll) going to be usable on both .net core and full .net 4.6 apps ?
Yes, this is the correct way. If you look into any ASP.NET Core projects, like Logging, you will find
"buildOptions": {
"keyFile": "../../tools/Key.snk"
...
}
in project.json file, and Key.snk in Tools folder. You also may check .NET Core - strong name assemblies issue.

How to create nuget package (dll) for standard framework and .Net Core?

I want to create data small data access library for Sql Server that wraps standard Sql Client classes, and publish it to NuGet. I want to use this NuGet package both in standard and .Net core apps.
I created class library project with some data access code (it uses System, System.Core, and System.Data) and published it to nugget. I have added System, System.Core, and System.Data as NuGet framework dependencies.
UPDATE - described problems both in RC1 and RC2
In RC1 version it works with 4.6 framework, but I had to remove DNX 5 from package.json.
In RC2 version it works with ASPNET Core (.Net Framework) projects, but when I create ASPNET Core (.Net Core), compilation fails:
Error NU1002 The dependency does not support framework .NETCoreApp,Version=v1.0.
Is there any way to create nugget package that works in both versions?
Since .NET Core 1.0 was just released this week, I'll answer the question in the context of the latest version of the framework.
If you are attempting to target both ASP.NET Core 1.0 and .NET 4.5 you have to define both frameworks separately, including all dependencies each framework build would require.
Here is an example project.json file for a class library project that targets .NET Core Standard 1.5, ASP.NET Core 1.0, .NET 4.5, and .NET 4.0 and has a dependency on the System.Linq.Expressions namespace:
{
"version": "1.0.0-*",
"frameworks": {
"netstandard1.5": {
"imports": "dnxcore50",
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.Linq.Expressions": "4.1.0"
}
},
"netcoreapp1.0": {
"imports": "dnxcore50",
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"System.Linq.Expressions": "4.1.0"
}
},
"net45": {
"frameworkAssemblies": {
"System.Linq.Expressions": ""
}
},
"net40": {}
}
}
With Visual Studio 2017, you can create a .NET Standard project library. You can select the version in the properties of the project.
Such project looks like this :
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
<AssemblyName>MyAssembly</AssemblyName>
<PackageId>MyPackage</PackageId>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<Copyright>Copyright</Copyright>
<Description>Simple Library</Description>
<Authors>Me</Authors>
<AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>1.1.0</FileVersion>
<Version>1.1.0</Version>
<Company>MyCompany</Company>
</PropertyGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
</Project>
In IDE, you have the pack command on the right click menu or you can type this :
msbuild /t:pack myproject.csproj
Only one package.... to rule them all and... :)

How do I use System.Data in a .NET Core RC2 console app (Linux, Debian 8)?

I've installed .NET Core RC2 on a Debian 8 amd64 system and would like to test if it's possible to query an instance of Microsoft SQL Server.
So I'd like to add to my project a dependency on the System.Data.SqlClient assembly.
Presently my project file created by running the dotnet new CLI tool looks like this:
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0-rc2-3002702"
}
},
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
}
Using this answer to a similar query, I was able to add a reference to System.Data.Common changing the
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50"
}
}
fragment to
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50",
"dependencies": {
"System.Data.Common": "*"
}
}
}
which made dotnet restore use NuGet to download a bunch of stuff.
I then tried to change that fragment to read
"frameworks": {
"netcoreapp1.0": {
"imports": "dnxcore50",
"dependencies": {
"System.Data.SqlClient": "*"
}
}
}
but NuGet says it's
Unable to resolve 'System.Data.SqlClient' for '.NETCoreApp,Version=v1.0'.
If I change the version string to read "4.1.0-rc3-*" the error message just gets more specific:
Unable to resolve 'System.Data.SqlClient (>= 4.1.0-rc3)' for '.NETCoreApp,Version=v1.0'.
What I'm puzzled about is that the NuGet package gallery dedicated to .NET Core explicitly lists System.Data.SqlClient as available.
So what could I do to add a reference to System.Data.SqlClient assembly to my project and have NuGet download it?
On a side note, I'm currently playing around in a plain console with only the dotnet CLI tool. Is there any way to manage project dependencies for a .NET Core project without resorting to installing IDEs?
Like poke already annotated in the comment is correct. Specify a version to System.Data.SqlClient makes your restore happy ;)
Why is that? System.Data.SqlClient exists in the http://nuget.org gallery. Not specifying a version ("") is not allowed outside of the boundaries of a project (like a nuget feed package) and specifying solely an star "*" (you should never do that, it allows breaking changes) restore the highest available version. Since there is no stable, star will not find anything (there is some magic with the dashes behind). The RC2 version of that library is the mentioned 4.1.0-rc2-24027 and when you ask with 4.1.0-rc2-* it will take the highest of the RC2 builds (but there is only one). In comparison System.Data.Common has a public release on nuget.org for the Universal Windows Platform and is found for that reason.
The RC3 is the next release and only available on developer feeds from the .NET Core and ASP.NET Core team and not the public nuget feed. You should not play with them.
if you are in project.json file, the intellisense guide you now if you have updated the Visual studio with the latest tooling avaiable..
I added the following in the dependencies element and it works perfectly..
"System.Data.SqlClient": "4.1.0-rc2-24027",
On my MINT 19 Tara I do not use System.Data.SqlClient but the newer version Microsoft.Data.SqlClient.
Check nuget
After typing the .NET-cli command, the package was downloaded and the reference was added to the project csproj file.
In the code, use the newer namespace:
using Microsoft.Data.SqlClient;
The "regular" code works fine:
public static void CreateCommand(string queryString, string connectionString)
{
using (SqlConnection connection = new SqlConnection(
connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
command.Connection.Open();
SqlDataReader reader = command.ExecuteReader() ;
while( reader.Read() ) {
for (int i = 0; i < reader.FieldCount; i++)
{
Console.WriteLine( "Column name={0}, Value={1}",
reader.GetName(i),
reader.GetValue(i) );
}
}
}
}

Categories

Resources