Bryan
Bryan

Reputation: 1471

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

I'd like to run a hello-world console app using the .NET CoreCLR.

So far, my code is as follows.

// Program.cs

using System;

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

 

// project.json

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

I'm attempting to run this project using the commands:

dnvm use 1.0.0-beta8 -r coreclr
dnx run

However this yields the following error:

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\004dnxCoreHelloWorld\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\004dnxCoreHelloWorld\Program.cs(5,18): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.Object' is not defined or imported
d:\rp\study\004dnxCoreHelloWorld\Program.cs(7,16): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.Void' is not defined or imported
d:\rp\study\004dnxCoreHelloWorld\Program.cs(9,31): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.String' is not defined or imported
d:\rp\study\004dnxCoreHelloWorld\Program.cs(9,13): DNXCore,Version=v5.0 error CS0518: Predefined type 'System.Object' is not defined or imported
d:\rp\study\004dnxCoreHelloWorld\Program.cs(9,13): DNXCore,Version=v5.0 error CS0103: The name 'Console' does not exist in the current context
d:\rp\study\004dnxCoreHelloWorld\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...

Upvotes: 3

Views: 1671

Answers (2)

pg0xC
pg0xC

Reputation: 1266

Project is missing System.Console dependency

This is how frameworks/dependencies sections may look like:

"dependencies": {
},

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

This is I think a minimal set.

to run use:

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

Upvotes: 6

Victor Hurdugaci
Victor Hurdugaci

Reputation: 28425

For CoreCLR, you need a reference to System.Runtime either directly or transitively.

So your project.json file should be:

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

Upvotes: 0

Related Questions