将 .NET Core 应用程序发布为可移植的可执行文件 [英] Publish .NET Core App As Portable Executable
问题描述
我有一个简单的 .net 核心应用程序并通过以下命令发布它:
dotnet publish -c Release -r win10-x64
SqlLocalDbStarter.csproj
<属性组><OutputType>Exe</OutputType><TargetFramework>netcoreapp2.1</TargetFramework></PropertyGroup><项目组><PackageReference Include="Microsoft.Win32.Registry" 版本="4.5.0"/></项目组></项目>
当发布过程完成 dotnet 在 binRelease
文件夹中创建 win10-x64
文件夹然后打开它后,该文件夹包含 publish
文件夹和一些dll 和 exe 文件.
我有一些问题:
- PE 应用程序需要哪个
exe
文件(在发布文件夹内/外)? - 为什么当我剪切
exe
文件并将其移动到其他地方时它不运行(没有消息)? - 如果我需要所有
dll
文件来运行应用程序,那么我有两个选项(在发布文件夹内部/外部),内部发布文件夹大小为 66 MB,但外部发布文件夹大小为 1 MB. - 我想要一个
exe
文件来运行我的程序,而不需要 dll 文件.
.NET Core 3.0
.NET Core 3.0 支持开箱即用.它将所有内容打包在一个 .exe
文件中(基本控制台应用程序约为 68 MB).有 PublishTrimmed=true
选项,通过分析静态代码引用并从最终构建中排除未使用的框架程序集,可以将大小减小到 ~28 MB.
要配置 single exe
构建,请编辑您的 csproj
文件:
<RuntimeIdentifier>win-x64</RuntimeIdentifier><PublishSingleFile>true</PublishSingleFile></PropertyGroup>
或在带有 csproj
文件的文件夹中的命令行上:
dotnet publish -r win-x64 -p:PublishSingleFile=true
有关更多详细信息,请参阅 Gopi 的精彩回答.
独立工具
Warp
(感谢 Darien Shannon 在评论中提及)和 dotnet CoreRT
.两者都适用于以前版本的 .Net Core
扭曲
它是一个类似于ILMerge 用于经典的 .NET Framework
.这是非常容易使用.对于基本的控制台应用程序,它可以生成 .exe
约 35 MB 不带摇树器和约 10-15 MB 的摇树器.
Dotnet CoreRT
现在,您可以尝试使用 dotnet 将应用程序预编译为原生单文件可执行文件CoreRT
项目.我说尝试"是因为文档说:
该项目处于开发的早期阶段.
尽管如此,它至少适用于简单的应用程序.在此处查看示例.根据其描述,您需要在项目文件夹中运行以下命令:
dotnet new nuget
<块引用>
这会将 nuget.config 文件添加到您的应用程序.打开文件并在下面的元素中添加以下内容:
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json"/><add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3"/>
然后运行:
dotnet 添加包 Microsoft.DotNet.ILCompiler -v 1.0.0-alpha-*
然后运行:
dotnet publish -r win-x64 -c release
<块引用>
完成后,您可以在根文件夹中找到本机可执行文件/bin/x64//netcoreapp2.0/publish/
下的项目I have a simple .net core app and publish it by following command:
dotnet publish -c Release -r win10-x64
SqlLocalDbStarter.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="4.5.0" />
</ItemGroup>
</Project>
When publish process finished dotnet create win10-x64
folder at binRelease
folder then after open it the folder contains publish
folder and some dll and exe files.
There are some issue for me:
- Which one of
exe
files (inside / outside publish folder) i need to PE app? - Why when i cut
exe
file and move it in other place it doesn't run (without message)? - If I need all of
dll
files to run application, so there are tow options for me (inside / outside publish folder), inside publish folder size is 66 MB but outside publish folder is 1 MB. - I want to have a one
exe
file to run my program without dll files.
.NET Core 3.0
.NET Core 3.0 supports it out of the box. It packs all stuff in one .exe
file (~68 MB for a basic console app). There is PublishTrimmed=true
option that can decrease size to ~28 MB by analyzing static code references and excluding unused framework assemblies from the final build.
To configure single exe
build edit your csproj
file:
<PropertyGroup>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
or on the command line in a folder with csproj
file:
dotnet publish -r win-x64 -p:PublishSingleFile=true
For more details see a great answer given by Gopi.
Standalone utils
Warp
(thanks to Darien Shannon for mentioning it in the comment) and dotnet CoreRT
. Both work with previous versions of .Net Core also
Warp
It is a tool similar to ILMerge for the classic .NET Framework
. It is very easy to use. For the basic console app, It can produce .exe
~35 MB without tree shaker and around 10-15 MB with tree shaker.
Dotnet CoreRT
For now, you can try to pre-compile the application into a native single-file executable using dotnet CoreRT
project. I'm saying "try" because documentation says:
This project is in the early stages of its development.
Nevertheless, it works at least for simple applications. See the sample here. According to its description, you need to run the following command in the project folder:
dotnet new nuget
This will add a nuget.config file to your application. Open the file and in the element under add the following:
<add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
Then run this:
dotnet add package Microsoft.DotNet.ILCompiler -v 1.0.0-alpha-*
Then run this:
dotnet publish -r win-x64 -c release
Once completed, you can find the native executable in the root folder of your project under /bin/x64//netcoreapp2.0/publish/
这篇关于将 .NET Core 应用程序发布为可移植的可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!