如何在 .NET CoreCLR 上为 运行 构建一个 hello-world 控制台应用程序?

How do I build a hello-world console app to run on the .NET CoreCLR?

我想 运行 使用 .NET CoreCLR 的 hello-world 控制台应用程序。

至此,我的代码如下。

// Program.cs

using System;

namespace Study
{
    public class Program
    {
        public void Main()
        {
            Console.WriteLine("Hello world!");
        }
    }
}

// project.json

{
  "frameworks": {
    "dnxcore50": { }
  }
}

我正在尝试 运行 这个项目使用命令:

dnvm use 1.0.0-beta8 -r coreclr
dnx run

但是这会产生以下错误:

Microsoft.Dnx.Compilation.CSharp.RoslynCompilationException: warning CS8021: No value for RuntimeMetadataVersion found. No assembly containing System.Object was found nor was a value for RuntimeMetadataVersion specified through options.
(1,11): DNXCore,Version=v5.0 error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
(2,11): DNXCore,Version=v5.0 error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
(3,11): DNXCore,Version=v5.0 error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
(1,58): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.String' is not defined or imported
(2,54): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.String' is not defined or imported
(3,67): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.String' is not defined or imported
d:\rp\study[=14=]4dnxCoreHelloWorld\Program.cs(1,7): DNXCore,Version=v5.0 error CS0246: The type or namespace name 'System' could not be found (are you missing a using directive or an assembly reference?)
d:\rp\study[=14=]4dnxCoreHelloWorld\Program.cs(5,18): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.Object' is not defined or imported
d:\rp\study[=14=]4dnxCoreHelloWorld\Program.cs(7,16): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.Void' is not defined or imported
d:\rp\study[=14=]4dnxCoreHelloWorld\Program.cs(9,31): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.String' is not defined or imported
d:\rp\study[=14=]4dnxCoreHelloWorld\Program.cs(9,13): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.Object' is not defined or imported
d:\rp\study[=14=]4dnxCoreHelloWorld\Program.cs(9,13): DNXCore,Version=v5.0 error CS0103: The name 'Console' does not exist in the current context
d:\rp\study[=14=]4dnxCoreHelloWorld\Program.cs(5,18): DNXCore,Version=v5.0 error CS1729: 'object' does not contain a constructor that takes 0 arguments
   at Microsoft.Dnx.Compilation.CSharp.RoslynProjectReference.Load(AssemblyName assemblyName, IAssemblyLoadContext loadContext)

...et cetera...

对于 CoreCLR,您需要直接或传递地引用 System.Runtime

所以你的 project.json 文件应该是:

{
    "frameworks": {
        "dnxcore50": { 
            "System.Runtime": "4.0.21-beta-*",
            "System.Runtime.Extensions": "4.0.11-beta-*"
        }
    }
}

缺少项目System.Console依赖项

frameworks/dependencies 个部分可能是这样的:

"dependencies": {
},

"frameworks": {
  "dnxcore50": {
    "dependencies": {
      "System.Console": "4.0.0-beta-*"
    }
  }
}

这是我认为的最小集合。

到运行使用:

dnvm use 1.0.0-beta8 -r coreclr
dnu restore
dnx run