A NuGet package
Most useful .NET libraries are NuGet packages. This lesson takes a real one, Newtonsoft.Json, from nuget.org to a Ring program in four steps - a project file, dotnet publish, the generator and new - and explains what to check for any other package.
What you will learn#
- what a NuGet package is and where its DLLs come from,
- a small project file that asks for a package, and
dotnet publishthat copies the package and what it needs into one folder, - wrapping the DLL and loading its classes,
- calling the package like any other class,
- what to look at when a package does not work at once.
What a package is#
A NuGet package is a zip file with one or more DLLs (one set for each version of .NET it supports), and a list of other packages that it needs. nuget.org is the public place where packages are kept: a name and a version number find one, for example Newtonsoft.Json 13.0.3.
You do not unzip a package by hand. The .NET SDK does it: a project file names the packages, and dotnet publish downloads them (once; they are kept on your computer afterwards) and copies all the DLLs that the program needs into one folder. That folder is what Ring uses.
The steps#
- Ask for the package in a project file.
- Publish:
dotnet publish Packages -c Release -o Packages/libscopies the package DLLs toPackages/libs. - Wrap:
ring wrapassembly.ring Packages/libs/Newtonsoft.Json.dll classeswrites the Ring classes. - Load and use:
dnLoad,dnImportFrom, thennew ClassName.
The earlier tutorial A library written in C# does steps 3 and 4 for a library that you build yourself. Here the library comes from somebody else, so step 1 and 2 are new.
The project file#
This is all that a project needs to get a package. It is in samples/UsingDotNet/tutorials/Packages:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<AssemblyName>Packages</AssemblyName>
<DebugType>none</DebugType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>PackageReference is the line that names the package and its version. The project also needs one source file, even when you write no C#, so there is an empty class:
// A project needs one source file to build; the program only wants the package that the project refers to.
namespace Tutorial.Packages { class Empty { } }To use another package, change the Include and Version values (nuget.org shows both on the page of every package), or run dotnet add Packages package Name and the SDK writes the line for you. You need the .NET SDK for this step only (dotnet --version prints its version); the finished program needs only the runtime and the DLLs.
The Ring program#
# a NuGet package - the project file asks for the package, dotnet publish copies its DLLs, the generator wraps them, and the classes are used
# (the project is in the folder Packages: Packages.csproj names Newtonsoft.Json, and Empty.cs is the one source file a project needs)
load "dotnet.ring"
cHere = currentdir()
cDll = cHere + "/Packages/libs/Newtonsoft.Json.dll"
cClasses = cHere + "/Packages/classes"
# STEP 1. Get the package. dotnet publish restores the packages that the project names (from nuget.org the first time, from the
# computer afterwards) and copies their DLLs, with the DLLs they need, into one folder: Packages/libs
if not fexists(cDll)
? "getting the package..."
system("dotnet publish Packages -c Release -o Packages/libs -nologo -v q > nul")
ok
if not fexists(cDll)
? "The package could not be restored. It is downloaded from nuget.org the first time, so the computer needs the internet once."
bye
ok
# STEP 2. Make Ring classes for the DLL of the package (the generator is a Ring program of this package: tools/dotnettools/wrapassembly.ring)
chdir("../../../tools/dotnettools")
system(exefilename() + " wrapassembly.ring " + cDll + " " + cClasses + " > nul")
chdir(cHere)
# STEP 3. Load the assembly and the classes of its namespaces. Newtonsoft.Json.Linq needs Newtonsoft.Json, and the loader finds it.
dnLoad(cDll)
dnImportFrom(cClasses + "/","Newtonsoft.Json.Linq")
dnImportFrom(cClasses + "/","Newtonsoft.Json")
func main
# STEP 4. Use the classes like any other
cText = '{"name":"Ann","age":31,"tags":["ring","dotnet"],"address":{"city":"Cairo"}}'
oObject = new JObject().Parse(cText) # Parse is a static method: it is called on an object, as in the tutorial about the C# library
? "name : " + oObject.Item("name").ToString()
? "age : " + oObject.Item("age").ToString()
? "city : " + oObject.SelectToken("address.city").ToString() # a path into the document
? "tags : " + oObject.Item("tags").Count
# a value is added with the name of the property and a JValue (the overload is chosen by the names of the parameters)
oObject.Add([ :propertyName = "country", :value = new JValue("Egypt") ])
? nl + "the document with the new property:"
? oObject.ToString()
# the other namespace of the package: JsonConvert turns a value into JSON text
? nl + "a text as JSON : " + new JsonConvert().SerializeObject("a b")
? "a number as JSON : " + new JsonConvert().SerializeObject(42) # a whole number that goes to an object is an Int32, so 42 is written as 42 (42.5 stays a double)name : Ann
age : 31
city : Cairo
tags : 2
the document with the new property:
{
"name": "Ann",
"age": 31,
"tags": [
"ring",
"dotnet"
],
"address": {
"city": "Cairo"
},
"country": "Egypt"
}
a text as JSON : "a b"
a number as JSON : 42How it works#
Why publish#
The DLL of the package is in the folder where the SDK keeps packages, in a sub-folder for each .NET version it supports, and the packages that it needs are somewhere else. dotnet publish chooses the right DLLs for the .NET of the project and puts them together in Packages/libs. A library that is copied alone may stop at its first call because a DLL that it needs is missing. Keep the folder together, and give it to the people who use your program (the tutorial Giving your program to others).
The DLL called Packages.dll that is also in the folder belongs to the empty project and is not used.
One DLL, several namespaces#
Newtonsoft.Json has the namespaces Newtonsoft.Json (for example JsonConvert) and Newtonsoft.Json.Linq (JObject, JArray, JValue). The generator writes one loader for each namespace, and dnImportFrom(folder,"Newtonsoft.Json.Linq") also loads the namespace that it needs. The program loads the other one too, because it uses JsonConvert from it.
Static methods and overloads#
Parse is a static method of JObject: Ring calls it on an object, new JObject().Parse(cText). Add has several versions, so it takes a list of named values, [ :propertyName = ..., :value = ... ], the same way as the methods of the C# library. A whole number that goes to an object parameter is an Int32 and any other number is a Double, so SerializeObject(42) writes 42 and SerializeObject(42.5) writes 42.5. Names of parameters are in the class files that the generator wrote (Packages/classes) and in the documentation of the package.
When a package does not work at once#
| What you see | What to check |
|---|---|
dotnet publish fails with Unable to find package | the name or the version, and that the computer is online the first time |
| the folder has no DLL with the name you expect | some packages are named after one name and hold DLLs with another: look in libs |
DotNet Error: Could not load file or assembly at a call | a DLL that the package needs is not next to the others: publish again and copy the whole folder |
| a method is not found | the class that you used is in the other namespace: load it with dnImportFrom |
System.DllNotFoundException | the package has a native library (.dll for Windows); it is in libs/runtimes/..., so keep that folder, and use the one for your system |
| the program needs a newer .NET | change TargetFramework in the project file to one that your computer has |
Before you build a program around a package, write a ten-line Ring program that calls the one method that you need. If that works, the rest is the same work.
Look at the license of a package before you give your program to others: most are free to use, but each has its own terms, and they are shown on its page at nuget.org.
What you learned#
- a package is a set of DLLs on nuget.org, and a project file names the ones you need,
dotnet publishcopies a package with what it needs into one folder,- wrap the DLL, load the namespaces with
dnImportFrom, and call the classes like any other, - a table of what to check when the first call fails.
Exercises#
- Read the
Countoftagsand print every tag with a loop (Item(0),Item(1)). - Read the
ageas a number, add one to it and print the result (oObject.Item("age").ToString()is a text;number(...)makes it a number). - Turn the text
[1,2,3]into aJArraywithnew JArray().Parse(...)and print itsCount. - Find another small package on nuget.org, name it in a second project file, publish it, wrap it and call one method.