How it works
The Ring process hosts the .NET runtime and talks to it through a small managed bridge. This page explains the pieces, so the rest of the documentation makes sense.
The layers#
The extension and the bridge#
ring_dotnet.dll is a normal Ring extension written in C. When the first .NET call is made it:
- finds the installed .NET runtime and starts it inside the Ring process with
hostfxr(no separate process, no IPC), - loads
RingDotNet.Bridge.dll, a small C# assembly, - keeps one entry point for requests:
dotnet_invoke(request). (The extension also has the functions for callbacks and for releasing objects, below.)
The bridge uses reflection. A Ring call such as oSB.Append(", ") becomes a request call [handle, "Append", ", "]; the bridge finds the right overload from the argument types, invokes it, and returns the result.
Handles#
.NET objects never leave .NET. Ring holds a handle (a number) inside a DotNetObject, and the bridge keeps the real object in a table. The generated wrapper classes are DotNetObjects with typed methods, so oSB.Append(...) and oSB.callMethod("Append",[...]) do the same thing.
Releasing objects#
A handle keeps its object alive, so handles must be released. Each wrapper object holds a managed pointer (a Ring pointer with a function that runs when Ring drops the value). When the wrapper is dropped, the pointer queues the handle, and the queued handles are released in groups of 32, so a program that makes thousands of objects does not grow without end. release() releases one handle at once, and dnFlush() releases the queued ones now (the tutorial Memory and the garbage collector).
The wire format#
Values travel as a compact tagged text: s5:Hello (string), n2:42 (number), l... (list), o3:17 (object handle), c... (callback), e... (error). Numbers that are not whole travel as the 8 bytes of the double, so there is no loss of precision; the number becomes the type of the parameter that receives it, and for a parameter of type object a whole number is an Int32 (Core concepts). Binary data (byte[]) travels as a Ring string: a Ring text can hold any byte. A ByteBuffer object (dnBuffer) is the one case where Ring holds a live array, for the methods that fill an array that you pass in.
Generated wrapper classes#
tools/dotnettools/gendotnet.ring reads the manifest (tools/dotnettools/dotnetmanifest.ring) and uses reflection to write one Ring class per .NET class into libraries/dotnetlib/generated/ (two files per namespace: a loader and the classes). These files are plain Ring source with real inheritance, properties (as attributes with getX/setX), events and enums. using { "System.IO" } loads one namespace on demand. A class index (a list of every class and its namespace, loaded the first time it is needed) lets the library import the right namespace automatically when a returned object has a type that is not loaded yet.
Results are wrapped by their real type#
When a method returns an object, the bridge sends the list of its type, its base types and its interfaces (the interface named after the class first, then the ones that inherit the most interfaces: a class that is not public is wrapped as its interface). dnWrapObject looks for the most specific generated class (importing its namespace if needed) and creates it. If no class exists the result is a plain DotNetObject — use callMethod and getProp on it. (A delegate type is a callback and has a class only when extension methods extend it.)
A class that is not public is all of its interfaces at once (ServiceProviderEngineScope is IServiceScope and IServiceProvider: ServiceProvider is a property of the first, GetService a method of the second), and sometimes no interface is named after it (the elements of a fluent API are IElement and IContainer: Text() is a method of the second). The bridge sends its interfaces that are not generic (up to six) as one group, and Ring wraps the object with a class made for that combination (its name starts with DnMix_). It derives from the first class and has the methods of the others merged in, so the object has the members of all of its interfaces. The class is made the first time it is needed and reused.
Callbacks: Ring code called from .NET#
dnCallback("...") passes Ring code where .NET expects a delegate. The bridge builds a delegate of the exact type, bound to a method of the bridge (a compiled expression for a delegate that a method cannot be bound to: ref and out parameters, more than 8 parameters); when .NET invokes it, the C extension runs the Ring function on the Ring thread and returns its result. Callbacks run synchronously during a call, which is why events from timers on other threads are ignored (see Rules and limits).
Threads#
| Thread | What runs there |
|---|---|
| The Ring thread | All Ring code, all calls into .NET and all callbacks. With dnUseDesktop() it is an STA thread and runs the message loop in Application.Run(). |
| .NET threads | Kestrel connections, tasks, timers. They never call Ring directly. The web server queues requests and Ring handles them in serveOne(). |
Runtime modes#
The runtime is configured with one of four runtimeconfig.json files: base (RingDotNet.Bridge), desktop (RingDotNet.Desktop), web (RingDotNet.Web) and all (RingDotNet.All). dnUseDesktop(), dnUseWeb() and dnUseAll() select the file before the runtime starts. dnUseData() only loads the Entity Framework assemblies; an AssemblyResolve handler in the bridge finds them next to it.
How a call is made#
A call from Ring to .NET is a request that Ring packs, the extension sends to the bridge, and the bridge runs by reflection.
- A method that returns a text, a number, a yes/no or nothing, and a property read or write, is packed and read in C: the generated method calls
dotnet_callwith the handle of the object, the name of the method (a ready wire item) and the arguments, and the extension builds the request, calls the bridge and returns the answer. - The bridge remembers the overload that it chose for a kind of arguments (the type; for a number, whole or not and its range; a text only when no overload could read it as an enum or another type), so a method with many overloads, such as
Math.Max, costs about the same as a method with one. - A method that returns an object: the extension answers the type and the handle, and Ring makes the wrapper object of the generated class with its guard. The library keeps one ready object per wrapper class and copies it, because a copy is cheaper than
new, which runs the class bodies andinitof every parent class. Every wrapper object declares one attribute for each property of its class and of its parent classes (that is howoBtn.Text = "x"reachessetText()), so a class with many properties, such as a Windows FormsButton, costs more than a small one. Keep a returned object in a variable (oButton = oForm.Controls.Item(0)) and use the variable, instead of asking for it again in a loop. - The arguments of a native call can be texts, numbers, lists, .NET objects and callbacks. A call with named arguments (
oMath.Round([ :value = 3.14159, :digits = 2 ])) and a method withoutorrefparameters are native too. - The bridge looks for a method in the object itself first, then in the public interfaces of its class (methods that the class implements explicitly), then among the extension methods of the loaded assemblies. The generator gives the Ring class of the extended type a method for each extension method, so the call reaches the bridge under the plain name; the first time, the generated method asks the bridge to load the assembly that holds the extension class.
- Native libraries of a package (
runtimes/<system>-<architecture>/nativenext to its DLL) are found when .NET asks for them, so a package such as SkiaSharp works without copying files. - Fewer crossings are better than faster crossings:
toList()copies a whole collection in one request, and a loop that reads 500 items withItem(i)makes 500 requests.
Packages#
| File | Role |
|---|---|
ring_dotnet.dll | C extension |
RingDotNet.Bridge.dll | reflection bridge |
RingDotNet.Web.dll | request queue and Kestrel host |
RingDotNet.Data.dll | RingDb and RingTypedDb (Entity Framework) |
Microsoft.EntityFrameworkCore*.dll, e_sqlite3.dll ... | Entity Framework Core and SQLite |
Where to go next#
- Core concepts: how .NET names, overloads, properties and enums look in Ring.
- The basics: the first tutorial chapter, if you prefer to learn by writing programs.