Wayne Werner Wayne Werner - 3 months ago 18
Linux Question

What is the simplest possible app you can build/run with dotnet core?

With mono, your "hello world" app can be super simple:

$ cat << EOF > hi.cs && mcs hi.cs && mono hi.exe
using System;

public class Hello{
public static void Main(){
Console.WriteLine("So simple!");
}
}
EOF


The official dotnet core samples are nowhere near as simple. Instead of requiring a compile file, execute file, it requires:


  • project metadata, in the form of a
    project.json
    file

  • running
    dotnet restore
    then
    dotnet build

  • the output, which is not just a single .exe



My understanding is that
dotnet core
is supposed to be .NET for Linux and is maybe trying to pull Mono in. Is that a correct assumption? If so, is there a simpler hello world application that one can make with dotnet core (or something else from dotnet whatever), or is the sample project, with the several commands and multi-file output the simplest that dotnet can offer?

Answer

dotnet is not just a compiler and a runtime, like Mono is, it's also a package manager and publishing platform. And those other parts are not meant to be optional. So the simplest reasonable source really does include project.json and running it requires dotnet restore (you don't need dotnet build, dotnet run calls that automatically).

If you really wanted to avoid using project.json (again, a bad idea), you would have to fight dotnet every step of the way, you're really not meant to do this. It would require something like this:

cat << EOF > Program.cs && dotnet /usr/share/dotnet/sdk/1.0.0-preview2-003121/csc.dll -nologo -r:"/usr/share/dotnet/shared/Microsoft.NETCore.App/1.0.0/System.Private.CoreLib.dll" Program.cs
using System;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("Hello World!");
    }
}
EOF
cat << EOF > Program.runtimeconfig.json && dotnet Program.exe
{
  "runtimeOptions": {
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "1.0.0"
    }
  }
}
EOF