生成.Net Core作为EXE而不是DLL [英] Build .Net Core as an EXE not a DLL

查看:352
本文介绍了生成.Net Core作为EXE而不是DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将.NET Core项目构建为EXE而不是DLL,以便可以执行。

I want to build a .NET Core project as a EXE and not a DLL so it can be executed.

此处的答案无效:如何运行.Net Core dll?

以下是示例代码:

using System;

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

这是我的project.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"
    }
  }
}

我当前正在使用VSCode,每当我使用构建任务来构建项目,或者运行 dotnet restore 时,我都会得到一个 .dll 在我的 bin / Debug 文件夹中。

I'm currently using VSCode, whenever I build the project with the build task, or run dotnet restore I just get a .dll in my bin/Debug folder.

如何构建.NET Core应用程序作为一个exe文件?

How do you build a .NET Core application as an exe?

奖金:我愿意,它将在Mac或其他设备上运行吗?

Bonus: I do, will that run on a mac or other device?

推荐答案

要生成EXE而不是DLL,您需要自包含的部署。您当前正在做的是与框架相关的部署。
要将您的文件转换为自包含文件,请在project.json文件中执行以下步骤。

To produce an EXE instead of a DLL, you need a self-contained deployment. 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.


  1. 删除类型:平台

  2. 添加运行时 部分进行操作应用程序支持的系统。

  1. Remove "type": "platform".
  2. Add a "runtimes" section for the operating systems your app supports.

构建时,传入目标操作系统。例如。 dotnet build -r osx.10.10-x64

When you build, pass in the target operating system. E.g. dotnet build -r osx.10.10-x64.

这是生成的project.json

This is the resultant 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": {}
  }
}

另请参见: https://docs.microsoft .com / zh-CN / dotnet / articles / core / deploying /#self-contained-deployments-scd

这篇关于生成.Net Core作为EXE而不是DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆