在 .NET Core RC2 中构建 .exe 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37390798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 09:48:06  来源:igfitidea点击:

Build .exe file in .NET Core RC2

.netasp.net-core.net-corepackage.json.net-core-rc2

提问by Johannes Heesterman

Every time I build a project using the new .NET Core RC2 templates I am notprovided with a runnable .EXEfile. If I hit F5 for debugging my console application it runs fine through the

每次我使用新的 .NET Core RC2 模板构建项目时,我都没有提供可运行的 . EXE文件。如果我按 F5 来调试我的控制台应用程序,它会通过

C:\Program Files\dotnet\dotnet.exe 

application. And if I use the

应用。如果我使用

dotnet run 

command in the folder, it runs fine as well. But I see no way to run the application without the .NET Core CLI tools.

文件夹中的命令,它也运行良好。但是我认为没有 .NET Core CLI 工具就无法运行应用程序。

The contents of my

我的内容

 bin\Debug\netcoreapp1.0\

folder looks like this:

文件夹看起来像这样:

Bin folder structure

bin文件夹结构

As you can see there is no .EXEfile available. Just the dll.

如您所见,没有 . EXE文件可用。只是dll。

Am I overlooking something? Or is there something wrong with my project.json file?

我是否忽略了什么?还是我的 project.json 文件有问题?

{
  "version": "1.0.0-*",
  "buildOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "Microsoft.NETCore.App": {
      "type": "platform",
      "version": "1.0.0-rc2-3002702"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": "dnxcore50"
    }
  }
}

Thanks!

谢谢!

采纳答案by Kévin Chalet

There are actually 2 app models in .NET Core:

.NET Core 中实际上有 2 个应用程序模型:

  • Portable apps: heavily inspired by "DNX console apps", these apps don't produce .exefiles and are instead executed by the .NET Core shared runtime (whose version is defined by the Microsoft.NETCore.Apppackage, thanks to its special type: platformattribute). The corresponding .NET Core runtime must be installed on the machine to be able to use portable apps. If the exact version cannot be found, an exception is thrown when running dotnet run.

  • Standalone apps: standalone apps are really similar to good old .NET console apps as they produce .exefiles. The .NET Core runtime doesn't have to be installed on the machine, because it is directly embedded with the application itself.

  • 便携式应用程序:深受“DNX 控制台应用程序”的启发,这些应用程序不生成.exe文件,而是由 .NET Core 共享运行时执行(Microsoft.NETCore.App由于其特殊type: platform属性,其版本由包定义)。必须在计算机上安装相应的 .NET Core 运行时才能使用便携式应用程序。如果找不到确切的版本,运行时会抛出异常dotnet run

  • 独立应用程序:独立应用程序与旧的 .NET 控制台应用程序非常相似,因为它们生成.exe文件。.NET Core 运行时不必安装在机器上,因为它直接嵌入到应用程序本身中。

You're currently using the first model. To use the standalone model, you need to tweak your project.json:

您目前正在使用第一个模型。要使用独立模型,您需要调整您的project.json

  • Add a runtimessection to list the environments your app will target (e.g win7-x64or ubuntu.14.04-x64). You can find the complete list here.
  • Remove the Microsoft.NETCore.Appdependency. You can replace it by this package instead: "NETStandard.Library": "1.5.0-rc2-24027".
  • 添加一个runtimes部分以列出您的应用程序将针对的环境(例如win7-x64ubuntu.14.04-x64)。您可以在此处找到完整列表。
  • 删除Microsoft.NETCore.App依赖。你可以用这个包代替它:"NETStandard.Library": "1.5.0-rc2-24027"

Here's an example of a standalone app:

这是一个独立应用程序的示例:

{
  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true,
    "warningsAsErrors": true
  },

  "dependencies": {
    "Microsoft.Extensions.Configuration.Binder": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.CommandLine": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc2-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final",
    "Microsoft.Extensions.DependencyInjection": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc2-final",
    "NETStandard.Library": "1.5.0-rc2-24027"
  },

  "frameworks": {
    "net451": { },

    "netcoreapp1.0": {
      "dependencies": {
        "System.Net.Ping": "4.0.0-rc2-24027"
      },

      "imports": [
        "dnxcore50",
        "dotnet5.6",
        "portable-net451+win8"
      ]
    }
  },

  "runtimes": {
    "win7-x64": { }
  }
}

回答by zwcloud

The answer is in the documentationwith complete steps now.

答案在文档中,现在有完整的步骤。

You can create two types of deployments for .NET Core applications:

  1. Framework-dependent deployment
  2. Self-contained deployment

您可以为 .NET Core 应用程序创建两种类型的部署:

  1. 依赖框架的部署
  2. 独立部署

For a runnable .EXE file, the Self-contained deploymentshould be used.

对于可运行的 .EXE 文件,应使用自包含部署

回答by Hyman Miller

To create a runnable application from a .NET Core console application you can use the dotnet tool. Just run in your project directory:

要从 .NET Core 控制台应用程序创建可运行的应用程序,您可以使用dotnet 工具。只需在您的项目目录中运行:

dotnet publish --runtime win7-x64

This creates a standalone app (self-contained deployment; includes all necessary libraries consuming at least 60MB on your disk). Of course you can also choose other runtimes, like osx.10.11-x64or ubuntu.16.04-x64.

这将创建一个独立的应用程序(自包含部署;包括所有必要的库,在您的磁盘上至少消耗 60MB)。当然,您也可以选择其他运行时,例如osx.10.11-x64ubuntu.16.04-x64

If you used the default configuration (New Project -> Console App (.NET Core)), there is no modification of any configuration file necessary.

如果使用默认配置 ( New Project -> Console App (.NET Core)),则无需修改任何配置文件。

回答by Shankar

step 1: remove "type": "platform",from Project.json under frameworkssection

第 1 步:"type": "platform",从 Project.jsonframeworks部分下删除

step 2: add run time section to your project.json. Note each section is separeted by a comma. Add your runtime. below is just an example for win 10.

第 2 步:将运行时部分添加到您的 project.json。请注意,每个部分都用逗号分隔。添加您的运行时。下面只是win 10的一个例子。

"runtimes": {
       "win10-x64": {}      
     }

Step 3: dotnet restore command on your project. ( open cmd, go to your project folder wherever src folder is there, run dotnet restor)

第 3 步:对您的项目执行 dotnet restore 命令。(打开 cmd,转到 src 文件夹所在的项目文件夹,运行 dotnet restor)

step 4: dotnet packstep 4: dotnet build -r win10-x64- or just build.

第 4dotnet pack步:第 4步:dotnet build -r win10-x64- 或者只是构建。

Step 5: you can notice .exe created under debug/netcore/win10/

第五步:可以看到在debug/netcore/win10/下创建的.exe

回答by Serj Sagan

In ASP.NET Coretry changing your app typeto default, in project.json:

ASP.NET Core尝试将您的应用程序更改typedefault,在project.json

"Microsoft.NETCore.App": {
  "type": "default",
  "version": "1.0.0-*"
}