xtianbetz / dotnet-hello-json

Demo .NET/C# App using Newtonsoft.JSON on Linux and Raspberry Pi

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dotnet CLI example with JSON

Overview

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.

Building and Running In Various Ways

Local Builds using the dotnet CLI

The following commands can build/run the application:

dotnet build
dotnet run
dotnet build -c Release
dotnet run -c Release

ARM Builds using the dotnet CLI

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"

Cross-platform builds using msbuild

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.

How this repo was made

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

Resources

About

Demo .NET/C# App using Newtonsoft.JSON on Linux and Raspberry Pi

License:MIT License


Languages

Language:C# 100.0%