RingDotNetfor Ring

Giving your program to others

A program that works on your computer is not finished until it works on somebody else's. This lesson turns a RingDotNet program into a folder with a program file that runs without Ring, explains what must be on the other computer, and adds a self check that says what is missing instead of crashing.

What you will learn#

Getting started installed the package for you. This lesson is about the people who only use your program.

The program#

samples/UsingDotNet/tutorials/ch13_distribution.ring
# giving your program to others - a self check that a program made with Ring2EXE can run when it starts
#   ring ring2exe.ring ch13_distribution.ring -dist -dotnet        (the page explains the steps)
load "dotnet.ring"

using {
	"SQLitePCL"
	"Microsoft.Data.Sqlite"
	"System"
	"System.IO"
}

func main

	? "Self check of RingDotNet"

	# 1. The .NET runtime. The program can not start without it, so a distributed program checks the version first and says clearly what to
	#    install. (The bridge needs .NET 6 or newer; the files of RingDotNet are made for .NET 9.)
	cVersion = dnVersion()                                   # a text like ".NET 9.0.15"
	cNumber = substr(cVersion,substr(cVersion,"NET ") + 4)    # "9.0.15"
	nMajor = number(left(cNumber,substr(cNumber,".") - 1))
	? nl + "1. the .NET runtime is version 6 or newer: " + (nMajor >= 6)

	# 2. The files of RingDotNet next to the program (the bridge and the libraries that Ring2EXE copies)
	cFolder = dnRequest("bridgefolder",[])
	? "2. the bridge assembly is in the program folder: " + new File().Exists(cFolder + "/RingDotNet.Bridge.dll")
	? "   the folder has the runtime configuration files: " + (new File().Exists(cFolder + "/RingDotNet.Bridge.runtimeconfig.json") and new File().Exists(cFolder + "/RingDotNet.Web.runtimeconfig.json"))

	# 3. The database libraries (Entity Framework and SQLite) with their native part: open a database in memory
	? "3. SQLite works: " + sqliteWorks()

	# 4. The web runtime. Programs that use the web server (the tutorial "A web service with a database") need the ASP.NET Core runtime on the computer; it is a separate
	#    runtime next to the .NET runtime. The folder of the .NET runtime tells where to look.
	? "4. the ASP.NET Core runtime is installed (needed only for web programs): " + aspNetInstalled()

	# 5. Writing next to the program is often not allowed (a program in "Program Files"): settings and data go to a folder of the user.
	#    The folder may not exist yet: the program makes it, and that shows that it can write there.
	cData = new Environment().GetFolderPath([ :folder = "ApplicationData", :option = "Create" ])          # "Create": made when it is missing (on Linux it often is)
	? "5. the folder for the data of the program exists or was made: " + (cData != "" and new Directory().Exists(cData))

func sqliteWorks

	try
		dnLoad(dnRequest("bridgefolder",[]) + "/RingDotNet.Data.dll")      # it knows where the native SQLite library of this system is
		dnLoadName("SQLitePCLRaw.batteries_v2")               # the assembly that starts the native SQLite library
		(new Batteries_V2).Init()
		oConnection = (new SqliteConnection).@New("Data Source=:memory:")
		oConnection.Open()
		oCommand = oConnection.CreateCommand()
		oCommand.CommandText = "SELECT 40 + 2"
		xAnswer = oCommand.ExecuteScalar()
		oConnection.Dispose()
		return (xAnswer = 42)
	catch
		return False
	done

func aspNetInstalled

	try
		cRuntime = new RuntimeEnvironment().GetRuntimeDirectory()
		# ...\shared\Microsoft.NETCore.App\9.0.15\ : two folders up is "shared", the folder of all the shared runtimes
		oShared = new Directory().GetParent(cRuntime).Parent.Parent
		return new Directory().Exists(new Path().Combine([oShared.FullName,"Microsoft.AspNetCore.App"]))
	catch
		return False
	done
Output
Self check of RingDotNet

1. the .NET runtime is version 6 or newer: 1
2. the bridge assembly is in the program folder: 1
   the folder has the runtime configuration files: 1
3. SQLite works: 1
4. the ASP.NET Core runtime is installed (needed only for web programs): 1
5. the folder for the data of the program exists or was made: 1

This program is the self check. It is a good start for your own program: put its checks in a function at the start, and call it when the program starts.

The steps#

1. Build. In the folder of the program (here myprogram.ring), with the Ring of the package installed:

powershell
ring ring2exe.ring myprogram.ring -dist -dotnet

(ring2exe.ring is in ring/tools/ring2exe; give its path, or build ring2exe.exe once and use ring2exe myprogram.ring -dist -dotnet.) The option -dist prepares a folder for distribution and -dotnet adds the files of RingDotNet. Use -gui too for a program with a window, so that no console window opens behind it.

2. Look at the folder. target/windows now holds the program (myprogram.exe), ring.dll, the compiled program (ring.ringo), the extension (ring_dotnet.dll), the bridge and its libraries (RingDotNet.Bridge.dll, RingDotNet.Data.dll, RingDotNet.Web.dll and the runtime configuration files), the libraries for databases (Entity Framework and SQLite with e_sqlite3.dll), and System.Data.Odbc.dll for Access (the tutorial Microsoft Access databases). The list of files is in tools/ring2exe/libs/dotnet.ring; the folder is about 11 MB.

3. Test it without Ring. The only real test is on a computer or a folder where Ring is not in the PATH: open a command window, go to target/windows, and run myprogram.exe. The self check of this lesson, built this way and run from its own folder, says yes to every line.

4. Give it. Copy the whole folder (a zip file is the simplest) and tell the user what to install first.

What the other computer needs#

NeedWhen
the .NET runtime, version 6 or newer (the files are made for .NET 9: install the 9.0 runtime)always. This is the runtime, not the SDK. Without it the program stops with ".NET runtime not found"
the ASP.NET Core runtime, same versionprograms with a web server, or that use Microsoft.AspNetCore, Microsoft.Extensions.* (configuration, data protection, rate limiting, routing...). It is installed together with the "ASP.NET Core Runtime" or the "Hosting Bundle"
the Windows Desktop runtimeprograms with Windows Forms or WPF windows (Windows only)
the Microsoft Access Database Engine (64-bit)programs that open Access files (the tutorial Microsoft Access databases)
the folder of dotnet publish of each NuGet package or C# library that the program usesthe DLLs and the runtimes folder with the native libraries go with the program, next to the DLL that dnLoad loads (the tutorial A NuGet package)
64-bit Windowsthe files are 64-bit

The runtimes are free downloads from dotnet.microsoft.com; tell users the exact name. If the runtime is installed in an unusual place, the variable DOTNET_ROOT points to it.

The self check#

A program that cannot start should say why, in words the user understands. The program above checks, one by one, that the runtime has the right version, that the bridge and its configuration files are next to the program, that SQLite works (RingDotNet.Data.dll is loaded first, because it knows where the native library of this system is, and then a database in memory is opened, which proves that the library is found), whether the ASP.NET runtime is installed, and that a folder for the data exists. When a line says 0, the program can print what to install and stop, instead of showing an error message of .NET. Do the check before you use the part that needs it, and only for what your program really uses.

Where the data goes#

A program in C:\Program Files usually may not write next to itself. Put settings and data in a folder of the user: Environment.GetFolderPath("ApplicationData") is the folder for the settings of the user (inside, make a folder with the name of your program), LocalApplicationData for data that stays on this computer, and CommonApplicationData for data of all users (writing there may need rights). Never write into the folder of the program itself.

Linux and macOS#

The same options work (ring ring2exe.ring myprogram.ring -dist -dotnet), and the program is made for the system it is built on. On these systems ring2exe takes the files from ring/lib, so setup.ring copies the managed files there when you install the package; the list takes the SQLite library of one system and processor (it says in the file which line to change for arm64 or an Intel Mac). The distribution for Linux and macOS is prepared in this way but not tested yet: build on the system you distribute for and run the result on a clean computer.

When something goes wrong#

MessageCause and answer
.NET runtime not found (install the .NET runtime or set DOTNET_ROOT)the runtime is missing or in an unusual place: install it, or set DOTNET_ROOT
Bridge assembly not foundthe folder is incomplete: RingDotNet.Bridge.dll must be next to ring_dotnet.dll; copy the whole target/windows folder
Can't find the .NET type ...the assembly of that class is not in the folder (a library of your own, a NuGet package): copy its DLL next to the program and load it with dnLoad (Your own assemblies)
A library or file cannot be loaded for a web or Access programthe ASP.NET Core runtime or the Access engine is missing, or its bitness does not match
Unable to load e_sqlite3the SQLite library of the right system is missing from the list
the program works on your computer and not on anotherrun the self check, then compare: the same runtime version? the same folder? a file next to the program that your computer has and the other does not?
Note

Do not copy the folder of the .NET runtime into your program folder to avoid the install: the runtime is shared, updated by Windows Update, and its license allows it only in a self-contained publish, which Ring2EXE does not make.

What you learned#

Exercises#

  1. Build the self check as selfcheck.exe and run it from a folder where Ring is not in the PATH.
  2. Add your own library (dnLoad) and see what ring2exe does not copy; add it to your distribution by hand.
  3. Make the program print "Install the .NET runtime 9.0 from dotnet.microsoft.com" and stop when line 1 says no.
  4. Make a zip file of the folder with ZipFile (the tutorial Files and folders) and check its size.

Where to go next#

This was the last tutorial of the last chapter. Keep Core concepts, the class reference and Rules and limits at hand, and read the applications for bigger programs.

RingDotNet — use .NET classes and methods from Ring programs. Screenshots and output on these pages come from the real samples.