Giter Club home page Giter Club logo

dotnet-hello-json's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.