将 .Net Core 构建为 EXE 而不是 DLL [英] Build .Net Core as an EXE not a 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.
这里的答案不起作用:How to run a .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
时,我都会在我的 bin 中得到一个
文件夹..dll
/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.
- 删除
"type": "platform"
. - 为您的应用支持的操作系统添加
"runtimes"
部分.
构建时,传入目标操作系统.例如.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/en-us/dotnet/articles/core/deploying/#self-contained-deployments-scd
这篇关于将 .Net Core 构建为 EXE 而不是 DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!