带有project.json的.Net控制台应用程序:类型在未引用的程序集中定义

时间:2016-05-10 13:27:47

标签: .net dnx

我正在尝试创建一个带有project.json结构的简单.Net控制台应用程序,使用VS2015选项控制台应用程序(包),使用外部NuGet包,并且我得到了参考错误。

的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"
      }
    }
  }
}

错误:

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'.

如果我创造一个&#34;传统&#34;没有project.json的控制台应用程序使用完全相同的Program.cs代码,它可以正常工作。

环境:

Windows 10 Pro
Visual Studio 2015 Professional 14.0.25123.00 Update 2
.Net 4.6.1
DNX 1.0.0-rc2-20221
编辑:我尝试恢复软件包并清理/重建,还从头开始创建项目,但那些没有帮助。

我做错了什么还是这个错误?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案。我不得不在frameworkAssemblies中添加System.Runtime,而不是依赖项。工作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"
      }
    }
  }
}