简体   繁体   English

Build.Net Core 作为 EXE 而不是 DLL

[英]Build .Net Core as an EXE not a DLL

I want to build a .NET Core project as a EXE and not a DLL so it can be executed.我想构建一个 .NET Core 项目作为 EXE 而不是 DLL 以便它可以执行。

The answer here did not work: How to run a.Net Core dll?这里的答案不起作用: How to run a.Net Core dll?

Here is the sample code:这是示例代码:

using System;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

Here is my project.json:这是我的项目。json:

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

I'm currently using VSCode, whenever I build the project with the build task, or run do.net restore I just get a .dll in my bin/Debug folder.我目前正在使用 VSCode,每当我使用构建任务构建项目或运行do.net restore时,我只会在bin/Debug文件夹中得到一个.dll

How do you build a .NET Core application as an exe?如何将 .NET 核心应用程序构建为 exe?

Bonus: I do, will that run on a mac or other device?奖励:我知道,它会在 Mac 或其他设备上运行吗?

To produce an EXE instead of a DLL, you need a self-contained deployment .要生成 EXE 而不是 DLL,您需要一个独立的部署 What you are currently doing is a framework-dependent deployment.您当前所做的是依赖于框架的部署。 To convert yours to self-contained, take the following steps in your project.json file.要将您的转换为自包含,请在您的 project.json 文件中执行以下步骤。

  1. Remove "type": "platform" .删除"type": "platform"
  2. Add a "runtimes" section for the operating systems your app supports.为您的应用支持的操作系统添加"runtimes"部分。

When you build, pass in the target operating system.构建时,传入目标操作系统。 Eg dotnet build -r osx.10.10-x64 .例如dotnet build -r osx.10.10-x64

This is the resultant project.json这是结果 project.json

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "version": "1.1.0"
        }
      },
      "imports": "dnxcore50"
    }
  },
  "runtimes": {
    "win10-x64": {},
    "osx.10.10-x64": {}
  }
}

See also: https://docs.microsoft.com/en-us/dotnet/articles/core/deploying/#self-contained-deployments-scd另见: https : //docs.microsoft.com/en-us/dotnet/articles/core/deploying/#self-contained-deployments-scd

I think most people got to this page because they picked .net core and they can't get an executable .exe file from their VS 2017 build.我认为大多数人进入这个页面是因为他们选择了 .net 核心并且他们无法从他们的 VS 2017 版本中获得可执行的 .exe 文件。 vs 2015 always used to make a .exe file for a console application. vs 2015 总是用于为控制台应用程序制作 .exe 文件。 Suddenly vs 2017 is fussing over this simple task.突然,vs 2017 正在为这个简单的任务大惊小怪。 With vs 2017 when you create the project you get 2 choices for a Console application.使用 vs 2017 创建项目时,您可以选择 2 个控制台应用程序。 One is Console App (.NET Core) and the other choice is Console App (.NET Framework).一个是 Console App (.NET Core),另一个选择是 Console App (.NET Framework)。 If you pick the .NET Core option you are going to have to move heaven and earth to get a .exe file from your Build.如果您选择 .NET Core 选项,您将不得不移动天地才能从您的 Build 中获取 .exe 文件。 The (.NET Core) option creates a .dll file from a Build. (.NET Core) 选项从 Build 中创建一个 .dll 文件。 If you pick the (.NET Framework) option it will build a xxxx.exe executable for you by default.如果您选择 (.NET Framework) 选项,它将默认为您构建 xxxx.exe 可执行文件。

You need to select publish from the build menu.您需要从构建菜单中选择发布。 Select a folder that you want to publish to.选择要发布到的文件夹。 Fill the target OS that you want to publish.填写您要发布的目标操作系统。 Save and press publish on the upper Right corner.保存并按右上角的发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM