.NET Core bindings to NNG:
NNG, like its predecessors nanomsg (and to some extent ZeroMQ), is a lightweight, broker-less library, offering a simple API to solve common recurring messaging problems, such as publish/subscribe, RPC-style request/reply, or service discovery. The API frees the programmer from worrying about details like connection management, retries, and other common considerations, so that they can focus on the application instead of the plumbing.
Using latest NNG release.
For list of missing APIs/features see
is:issue is:open label:enhancement.
Goals of nng.NETCore:
- Async first: async/await access to nng_aio and nng_ctx
- Native layer: P/Invoke in separate files/namespace. Don't like our high-level OO wrapper? Re-use the pinvoke and make your own. Also makes cross-platform-friendly pinvoke easier.
- Tests as Documentation: xUnit unit/integration tests in "plain" C# much like you'd write
- .NET Core friendly: Using
dotnetand targetting .NET Standard from the start
- Few surprises: Simple class heirarchy (more composition than inheritance), minimal exceptions, idiomatic C# that when reasonable is similar to original native code
Supports projects targetting:
- .NET Core App 1.0+
- .NET Standard 1.5+
SuppressUnmanagedCodeSecurityis used with .NET Standard 2.0+ for improved PInvoke performance
- .NET Framework 4.6.1+ (caveats)
- Windows Vista or later 32/64-bit
- macOS/OSX 10.?+ (built on 10.14)
- Linux x86_64 (built on Ubuntu 18.04)
- Linux ARM64/aarch64 (built on Debian 9/stretch)
Should be easy to add others that are supported by both .NET Core and NNG.
After installing the package and building, your output folder should have
runtimes/ directory containing native binaries.
On .NET Core/Standard use
NngLoadContext (or your own
AssemblyLoadContext) to load the appropriate native library and use NNG:
var path = Path.GetDirectoryName(typeof(Program).Assembly.Location); var ctx = new nng.NngLoadContext(path); var factory = nng.NngLoadContext.Init(ctx); // Use factory...
Build & Run
You should be able to build nng.NETCore for/on any platform supported by .NET Core:
dotnet test tests
You should be able to build the NNG native shared library for any platform supported by NNG:
- Download/clone NNG source
- On Windows, create Command Prompt suitable for Visual Studio:
- Run x64 Visual Studio Developer Command Prompt to create a 64-bit library (or x86 for 32-bit)
- OR, run
vcvars64.batin cmd.exe (or
- Make sure cmake and Ninja are in
mkdir build && cd build cmake -G Ninja -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release .. ninja
- Copy library to appropriate
System.Runtime.Loader is not available in .NET Framework, so the correct assembly must be loaded by some other means.
If your application is targetting .Net Framework 4.6+ and you get lots of:
message NETSDK1041: Encountered conflict between 'Reference:XXX\.nuget\packages\subor.nng.netcore\0.0.5\lib\netstandard2.0\Microsoft.Win32.Primitives.dll' and 'Reference:Microsoft.Win32.Primitives'. NETSDK1034: Choosing 'Reference:XXX\.nuget\packages\subor.nng.netcore\0.0.5\lib\netstandard2.0\Microsoft.Win32.Primitives.dll' because file version '4.6.26419.2' is greater than '4.6.25714.1'. <snip> XXX\Properties\AssemblyInfo.cs(8,12,8,25): error CS0246: The type or namespace name 'AssemblyTitleAttribute' could not be found (are you missing a using directive or an assembly reference?)
Try adding the following to your project:
<PropertyGroup> <DependsOnNETStandard>false</DependsOnNETStandard> </PropertyGroup>