Soh*_*deh 9 c# publish self-contained portable-executable .net-core
我有一个简单的.net核心应用程序,并通过以下命令发布它:
dotnet publish -c Release -r win10-x64
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
当发布过程完成dotnet win10-x64
在bin\Release
文件夹中创建文件夹然后打开后,该文件夹包含publish
文件夹和一些dll和exe文件.
对我来说有一些问题:
exe
我需要PE应用程序中的哪一个文件(内部/外部发布文件夹)?exe
文件并将其移动到其他地方时它不会运行(没有消息)?dll
文件来运行应用程序,那么我有两个选项(内部/外部发布文件夹),内部发布文件夹大小为66 MB但外部发布文件夹为1 MB.exe
文件来运行我的程序没有DLL文件.Alb*_*rtK 13
如上所述这里有两种两种类型的部署for .NET的核心应用和双方不允许得到一个单一的.exe
文件.因此,它尚未得到支持.正如Karel Zikmund在评论中指出的那样,它计划用于.NET Core 3.0.
目前我们至少有两种选择:( PublishTrimmed=true
感谢Darien Shannon在评论中提到它)和single exe
它是一个类似于ILMerge的经典工具csproj
.这是非常容易使用.我在控制台应用程序上尝试使用csproj
Windows x64 上的东西,它运行良好.
现在,您可以尝试使用Warp
项目将应用程序预编译为本机单文件可执行文件.我说"试试"因为文档说:
该项目处于早期发展阶段.
然而,它至少适用于简单的应用程序.请在此处查看示例.根据其描述,您需要在项目文件夹中运行以下命令:
<PropertyGroup>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
这会将nuget.config文件添加到您的应用程序中.打开文件并在元素下添加以下内容:
dotnet publish -r win-x64 -p:PublishSingleFile=true
Run Code Online (Sandbox Code Playgroud)
然后运行:
dotnet new nuget
Run Code Online (Sandbox Code Playgroud)
然后运行:
<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" />
Run Code Online (Sandbox Code Playgroud)
完成后,您可以在/bin/x64//netcoreapp2.0/publish/下找到项目根文件夹中的本机可执行文件.
.NET Core 3.0之前
dotnet publish -r win-x64 -c Release --self-contained
Run Code Online (Sandbox Code Playgroud)
自我解释:
因此这是正确的,我们最终得到一个包含我们的exe以及运行它所需的所有文件的文件夹,但是问题是,甚至需要运行一吨HelloWorld控制台应用程序。
.NET Core 3.0之后
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true
Run Code Online (Sandbox Code Playgroud)
所有这些操作都是运行我们的publish命令,但告诉它将其打包在一个文件中。您会注意到,我们不再指定自包含标志。这是因为假设如果您将其打包为单个exe,则将需要所有依赖关系。说得通。
一个整洁的exe文件!执行此操作后,将依赖项提取到一个临时目录,然后从那里运行所有内容。它实质上是我们先前发布文件夹的zip!我玩过很多游戏,说实话,它确实有效。没什么可说的。它只是工作。
文件大小和启动成本
修改csproj并添加PublishTrimmed = true。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
现在运行以下命令:
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true
Run Code Online (Sandbox Code Playgroud)
参考: