【2023年】最新 .NET 的单文件尝试

.NET8

根据官方更新:截至到2023年2月23日, .NET 8 Preview 1 AOT编译 在大小方面有重大进步。

.NET 7.NET 8 Preview 1
Linux x64 (with -p:StripSymbols=true)3.76 MB1.84 MB
Windows x642.85 MB1.77 MB

实际测试:

PS D:\Project\net8> dotnet publish -r win-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true -p:StripSymbols=true --self-contained true
MSBuild version 17.6.0-preview-23108-10+51df47643 for .NET
  正在确定要还原的项目…
  所有项目均是最新的,无法还原。
C:\Program Files\dotnet\sdk\8.0.100-preview.1.23115.2\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInf
erence.targets(287,5): message NETSDK1057: 你正在使用 .NET 的预览版。请参阅 https://aka.ms/dotnet-support-policy [D:\Project\net8\ne
t8.csproj]
  net8 -> D:\Project\net8\bin\Release\net8.0\win-x64\net8.dll
  net8 -> D:\Project\net8\bin\Release\net8.0\win-x64\publish\
PS D:\Project\net8> ls  -l .\bin\Release\net8.0\win-x64\publish\

    Directory: D:\Project\net8\bin\Release\net8.0\win-x64\publish

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---      2023/2/23/周四    10:31       11698126 net8.exe
-a---      2023/2/23/周四    10:29          10212 net8.pdb

结果变化很小,即便是加上 -p:StripSymbols=true 参数!!

原文参考:https://devblogs.microsoft.com/dotnet/announcing-dotnet-8-preview-1/#native-aot

.NET6 和 .NET7

半年前(2月20日)测试过,现在(2021年8月25日)再次使用最新的 .NET 6.0.0-preview.7 再次验证一下。

PS C:\Users\Administrator> dotnet --version
6.0.100-preview.7.21379.14

直接新建了一个默认项目:

PS D:\Project\net6> dotnet new  console
The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on D:\Project\net6\net6.csproj...
  正在确定要还原的项目…
  已还原 D:\Project\net6\net6.csproj (用时 75 ms)。
Restore succeeded.

默认单文件,一个Hello World! 60MB 60.2MB:

PS D:\Project\net6> dotnet publish -r win-x64 -p:PublishSingleFile=true --self-contained true
用于 .NET 的 Microsoft (R) 生成引擎版本 16.9.0-preview-21103-02+198f3f262
版权所有(C) Microsoft Corporation。保留所有权利。

  正在确定要还原的项目…
  所有项目均是最新的,无法还原。
  你正在使用 .NET 的预览版。请查看 https://aka.ms/dotnet-core-preview
  net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\net6.dll
  net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\
PS C:\Users\Administrator> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\net6.exe
Hello World!
PS D:\Project\net6> dir .\bin\Debug\net6.0\win-x64\publish\


    目录: D:\Project\net6\bin\Debug\net6.0\win-x64\publish


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----   2021/2/3 星期三      4:11        1304944 mscordaccore.dll
-a----  2021/2/20 星期六     11:09       60808321 net6.exe
-a----  2021/2/20 星期六     11:05           9404 net6.pdb

使用PublishTrimmedPublishReadyToRun参数后 21MB 13.6MB。

PS D:\Project\net6> dotnet publish -r win-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true -p:PublishReadyToRun=true --self-contained true
用于 .NET 的 Microsoft (R) 生成引擎版本 16.9.0-preview-21103-02+198f3f262
版权所有(C) Microsoft Corporation。保留所有权利。

  正在确定要还原的项目…
  所有项目均是最新的,无法还原。
  你正在使用 .NET 的预览版。请查看 https://aka.ms/dotnet-core-preview
  net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\net6.dll
  正在优化程序集的大小,这可能更改应用的行为。请务必在发布后进行测试。请参阅: https://aka.ms/dotnet-illink
  net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\
PS D:\Project\net6> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\net6.exe
Hello World!
PS D:\Project\net6> dir .\bin\Debug\net6.0\win-x64\publish\


    目录: D:\Project\net6\bin\Debug\net6.0\win-x64\publish


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----   2021/2/3 星期三      4:11        1304944 mscordaccore.dll
-a----  2021/2/20 星期六     11:18       21196983 net6.exe
-a----  2021/2/20 星期六     11:05           9404 net6.pdb

只使用PublishTrimmed参数,20MB 10.8MB。

PS D:\Project\net6> dotnet publish -r win-x64 -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained true
用于 .NET 的 Microsoft (R) 生成引擎版本 16.9.0-preview-21103-02+198f3f262
版权所有(C) Microsoft Corporation。保留所有权利。

  正在确定要还原的项目…
  所有项目均是最新的,无法还原。
  你正在使用 .NET 的预览版。请查看 https://aka.ms/dotnet-core-preview
  net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\net6.dll
  正在优化程序集的大小,这可能更改应用的行为。请务必在发布后进行测试。请参阅: https://aka.ms/dotnet-illink
  net6 -> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\
PS D:\Project\net6> dir .\bin\Debug\net6.0\win-x64\publish\


    目录: D:\Project\net6\bin\Debug\net6.0\win-x64\publish


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----   2021/2/3 星期三      4:11        1304944 mscordaccore.dll
-a----  2021/2/20 星期六     11:22       19057335 net6.exe
-a----  2021/2/20 星期六     11:05           9404 net6.pdb


PS D:\Project\net6> D:\Project\net6\bin\Debug\net6.0\win-x64\publish\net6.exe
Hello World!
PS D:\Project\net6>

最后:.NET6的重点当然不是PublishSingleFile,如果是小工具还是go有优势。 单文件与go已经没啥差别了。

参考:https://docs.microsoft.com/zh-cn/dotnet/core/deploying/single-file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值