This repo is a simple demo application built with dotnet that processes JSON. It was made to help me learn, understand, and teach using dotnet build tools on Linux without using any GUI or Visual Studio tools.
The instructions below were tested on a CentOS 7.7 build host and a Raspberry Pi 4 running Yocto version 3.0.
The following commands can build/run the application:
dotnet build
dotnet run
dotnet build -c Release
dotnet run -c Release
Publish a build for the "linux-arm" runtime. This will produce something that appears to be a native ARM binary.
dotnet publish -c Release -r linux-arm
Disable Globalization (optional, if needed)
cat bin/Release/netcoreapp3.1/linux-arm/publish/dotnet-hello-json.runtimeconfig.json | jq -r '.runtimeOptions |= . + {"configProperties":{"System.Globalization.Invariant":true}}' > dotnet-hello-json.runtimeconfig.json
mv dotnet-hello-json.runtimeconfig.json bin/Release/netcoreapp3.1/linux-arm/publish/dotnet-hello-json.runtimeconfig.json
Run the build on a raspberry pi:
tar -C bin/Release/netcoreapp3.1/linux-arm/publish/ -cvzf /tmp/dotnet-hello-json.tgz .
scp /tmp/dotnet-hello-json.tgz pi:/tmp
ssh pi "mkdir -p /usr/libexec/dotnet-hello-json &&
tar -C /usr/libexec/dotnet-hello-json -xzvf /tmp/dotnet-hello-json.tgz &&
ln -sf /usr/libexec/dotnet-hello-json/dotnet-hello-json /usr/bin/dotnet-hello-json &&
dotnet-hello-json"
You can use msbuild (included with mono) and target 'net48' (.NET Framework 4.8). The result will be a familiar .exe file.
msbuild -restore -p:TargetFramework=net48
msbuild -restore -p:Configuration=Release -p:TargetFramework=net48
You can now run the release build using mono and the recommended shell-script wrapper technique.
tar -C bin/Release/net48/ -cvzf /tmp/clr-dotnet-hello-json.tgz .
scp /tmp/clr-dotnet-hello-json.tgz pi:/tmp
ssh pi mkdir -p /usr/libexec/clr-dotnet-hello-json
ssh pi tar -C /usr/libexec/clr-dotnet-hello-json -xzvf /tmp/clr-dotnet-hello-json.tgz
ssh pi 'echo -en "#!/bin/sh\n/usr/bin/mono /usr/libexec/clr-dotnet-hello-json/dotnet-hello-json.exe \"\$@\"\n" > /usr/bin/clr-dotnet-hello-json'
ssh pi chmod +x /usr/bin/clr-dotnet-hello-json
ssh pi clr-dotnet-hello-json
There does not appear to be a way target the .NET Framework using the 'dotnet' CLI tool on Linux. In other words: you cannot use 'dotnet build' and friends to target .NET Framework and run on mono.
TODO: this repo has yet been tested with any native libraries which may require multiple native .so files for different architectures. PRs are welcome.
mkdir dotnet-hello-json
cd dotnet-hello-json/
dotnet new console
dotnet add package Newtonsoft.Json
Convert to Unix line endings:
unix2dos dotnet-hello-json.csproj
Finally, I edited Program.cs to create a mashup of HelloWorld and the Newtonsoft.JSON object deserialization example you can find here
- Official .NET guide Required reading. Just do it!
- Essential MSBuild: A Build Engine Overview for for .NET Tooling
- All about Target frameworks Target Frameworks
- SDK-style project definitions Moving to SDK-Style projects and package references in Visual Studio, part 2
- More about csproj changes for .NET Core Additions to the csproj format for .NET Core
- Package references (PackageReference) in project files
- Installing the dotnet tool on CentOS 7: CentOS 7 Package Manager - Install .NET Core
- Quickstart: Install and use a package using the dotnet CLI
- Couldn’t find a valid ICU package installed on the system?
- Deserialize an Object with Newtonsoft.JSON: Deserialize an Object
- Installing Mono on CentOS
- Running Mono Applications