在 .net 核心上查找我需要哪些依赖项的一般方法是什么?

what is the general way to find what dependencies do I need on .net core?

我正在尝试根据我在某些博客上找到的代码创建一个简单的 .net 核心应用程序。我在 Ubuntu 16.04。我的文件是:

project.json

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },
  "dependencies": {
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.Server.WebListener": "0.1.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0"
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.1"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

Program.cs

using Microsoft.Extensions.Configuration; // for using IConfiguration
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel;
using System.IO; // for using Directory

namespace ConsoleApplication
{
  public class Program
  {
    public static void Main(string[] args)
    {
      var config = new ConfigurationBuilder()
          .SetBasePath(Directory.GetCurrentDirectory())
          .AddJsonFile("hosting.json", optional: true)
          .Build();

      var host = new WebHostBuilder()
          .UseKestrel()
          .UseConfiguration(config)
          .UseContentRoot(Directory.GetCurrentDirectory())
          .UseIISIntegration()
          .UseStartup<Startup>()
          .Build();

      host.Run();
    }
  }
}

hosting.json

{
  "server.urls": "http://localhost:60000;http://localhost:60001"
}

我在执行 dotnet publish 时遇到的错误是:

error CS0246: The type or namespace name 'Startup' could not be found (are you missing a using directive or an assembly reference?)

此外,请记住,我正在寻找一种通用方法 来了解我错过了哪些依赖项。

真的没有了。您也许可以找到像 resharper 这样的工具,它知道官方 Microsoft 软件包,并会告诉您 "Microsoft.EntityFrameworkCore" 或您缺少的任何内容。然而,这是一个需要可靠解决的问题。在您的示例中,您缺少应该在代码中定义的 class 。它也可以通过包含一个定义了 Startup 的包来解决。即使假设是这样,它怎么知道选择正确的呢?可能有很多实现 Startup class 的包。 Foo.Startup、Bar.Startup、Microsoft.CrossPlatform.StartUp - 所有这些都可以以您编译代码的方式实现 Startup。工具如何知道哪些适合您的需求?一般来说,您会找到一个库来包含和添加它,而不是编写代码然后寻找您需要的实现。

您正在请求依赖项,但您并没有遗漏任何一项。您缺少的是 Startup class,您关注的博客忘记提及,或者您没有按照说明进行操作。

创建新 ASP.NET 核心项目的最简单方法是 运行 dotnet new -t webStartup class 是 here.