Ricardo Smania
Ricardo Smania

Reputation: 3139

.Net console application with project.json: Type is defined in an assembly that is not referenced

I am trying to create a simple .Net Console Application with project.json structure, using the VS2015 option Console Application (Package), using an external NuGet package, and I'm getting reference errors.

Program.cs:

using System.Net.Http;
using System.Threading.Tasks;
using Nito.AsyncEx;

namespace AsyncTest
{
    public class Program
    {
        public static void Main(string[] args)
        {
            AsyncContext.Run(() => MainAsync(args));
        }

        public static async void MainAsync(string[] args)
        {
            var urlContents = await AccessTheWebAsync();
        }

        static async Task<int> AccessTheWebAsync()
        {
            HttpClient client = new HttpClient();

            Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

            string urlContents = await getStringTask;

            return urlContents.Length;
        }

    }
}

project.json:

{
  "version": "1.0.0-*",
  "description": "AsyncTest Console Application",
  "authors": [ "ricsmania" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Nito.AsyncEx": "3.0.1"
  },

  "commands": {
    "AsyncTest": "AsyncTest"
  },

  "frameworks": {
    "dnx451": {
      "dependencies": {
        "Microsoft.Net.Http": "2.2.22",
        "System.Runtime": "4.0.0"
      }
    }
  }
}

And the errors:

DNX 4.5.1 error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
DNX 4.5.1 error CS0012: The type 'Action' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
DNX 4.5.1 error CS0012: The type 'Func<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

If I create a "traditional" Console Application without project.json using the exact same Program.cs code it works fine.

Environment:

Windows 10 Pro
Visual Studio 2015 Professional 14.0.25123.00 Update 2
.Net 4.6.1
DNX 1.0.0-rc2-20221

EDIT: I tried restoring packages and clean/rebuild, and also creating a project from scratch, but those didn't help.

Am I doing something wrong or is this a bug?

Upvotes: 1

Views: 135

Answers (1)

Ricardo Smania
Ricardo Smania

Reputation: 3139

I found out the solution. I had to add System.Runtime inside frameworkAssemblies, not dependencies. Working project.json:

{
  "version": "1.0.0-*",
  "description": "AsyncTest Console Application",
  "authors": [ "ricardo.smania" ],
  "tags": [ "" ],
  "projectUrl": "",
  "licenseUrl": "",

  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Nito.AsyncEx": "3.0.1"
  },

  "commands": {
    "AsyncTest": "AsyncTest"
  },

  "frameworks": {
    "dnx451": {
      "dependencies": {
        "Microsoft.Net.Http": "2.2.22"
      },
      "frameworkAssemblies": {        
        "System.Runtime": "4.0.0.0",
        "System.Threading.Tasks": "4.0.0.0"
      }
    }
  }
}

Upvotes: 1

Related Questions