當前位置: 妍妍網 > 碼農

NetBeauty2:讓你的.NET計畫輸出目錄更清爽

2024-03-13碼農

在.NET計畫開發中,隨著計畫復雜性的增加,依賴的dll檔也會逐漸增多。這往往導致輸出目錄混亂,不便於管理和部署。而NetBeauty2開源計畫正是為了解決這一問題而生,它能夠幫助開發者在獨立釋出.NET計畫時,將.NET執行時和依賴的dll檔移動到指定的目錄,從而讓輸出目錄更加幹凈、清爽。

unset unset 1. NetBeauty2簡介 unset unset

NetBeauty2是一個開源的.NET依賴庫整理工具,它的主要作用是在.NET計畫獨立釋出時,對輸出目錄進行整理和最佳化。透過NetBeauty2,開發者可以輕松地將.NET執行時和依賴的dll檔移動到指定的目錄,使得計畫的輸出目錄更加清晰、易於管理。

計畫倉庫地址: https://github.com/nulastudio/NetBeauty2 [1]

下圖為最佳化後輸出目錄(.NET執行時及參照依賴庫移到 libraries 目錄,目錄名可配置):

下圖為極限最佳化後輸出目錄(檢視 `--hiddens` [2] 選項使用)

再來對比下未使用前輸出目錄(震撼吧!.NET執行時及相關依賴庫全放在了根輸出目錄,.NET Framework可以配置 privatePath ,.NET Core可沒那麽方便):

unset unset 2. 支持情況 unset unset

NetBeauty 2 [3] NetCoreBeauty [4]
支持框架 .Net Framework .Net Core 3.0+ .Net Core 2.0+
支持部署模式 Framework-dependent deployment ( FDD ) Self-contained deployment ( SCD ) Framework-dependent executables ( FDE ) Self-contained deployment ( SCD )
支持作業系統 All win-x64 win-x86 win-arm64 (.NET 6+) linux-x64 linux-arm linux-arm64 osx-x64 osx-arm64 (.NET 6+)
Need Patched HostFXR No Yes(if use patch) Yes
Minimum Structure ~20 Files ~8 Files(if use patch) ~8 Files
How It Works `STARTUP_HOOKS` [5] `AssemblyLoadContext.Resolving` [6] `AssemblyLoadContext.ResolvingUnmanagedDll` [7] + `patched libhostfxr` [8] (if use patch) `additionalProbingPaths` [9] (if use patch) `patched libhostfxr` [10] `additionalProbingPaths` [11]
Shared Runtime Yes Possible If Using patched libhostfxr Alone

unset unset 3. 如何使用? unset unset

3.1. 準備工作

在你的.NET Core工程(需要釋出的主工程)添加Nuget包:

dotnet add package nulastudio.NetBeauty

開啟工程檔編輯( .csproj ):

<ProjectSdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<BeautySharedRuntimeMode>False</BeautySharedRuntimeMode>
<!-- beauty into sub-directory, default is libs, quote with "" if contains space -->
<BeautyLibsDirCondition="$(BeautySharedRuntimeMode) == 'True'">../libraries</BeautyLibsDir>
<BeautyLibsDirCondition="$(BeautySharedRuntimeMode) != 'True'">./libraries</BeautyLibsDir>
<!-- dlls that you don't want to be moved or can not be moved -->
<!-- <BeautyExcludes>dll1.dll;lib*;...</BeautyExcludes> -->
<!-- dlls that end users never needed, so hide them -->
<!-- <BeautyHiddens>hostfxr;hostpolicy;*.deps.json;*.runtimeconfig*.json</BeautyHiddens> -->
<!-- set to True if you want to disable -->
<DisableBeauty>False</DisableBeauty>
<!-- set to False if you want to beauty on build -->
<BeautyOnPublishOnly>False</BeautyOnPublishOnly>
<!-- DO NOT TOUCH THIS OPTION -->
<BeautyNoRuntimeInfo>False</BeautyNoRuntimeInfo>
<!-- set to True if you want to allow 3rd debuggers(like dnSpy) debugs the app -->
<BeautyEnableDebugging>False</BeautyEnableDebugging>
<!-- the patch can reduce the file count -->
<!-- set to False if you want to disable -->
<!-- SCD Mode Feature Only -->
<BeautyUsePatch>True</BeautyUsePatch>
<!-- App Entry Dll = BeautyDir + BeautyAppHostDir + BeautyAppHostEntry -->
<!-- see https://github.com/nulastudio/NetBeauty2#customize-apphost for more details -->
<!-- relative path based on AppHostDir -->
<!-- .NET Core Non Single-File Only -->
<!-- <BeautyAppHostEntry>bin/MyApp.dll</BeautyAppHostEntry> -->
<!-- relative path based on BeautyDir -->
<!-- .NET Core Non Single-File Only -->
<!-- <BeautyAppHostDir>..</BeautyAppHostDir> -->
<!-- <BeautyAfterTasks></BeautyAfterTasks> -->
<!-- valid values: Error|Detail|Info -->
<BeautyLogLevel>Info</BeautyLogLevel>
<!-- set to a repo mirror if you have troble in connecting github -->
<!-- <BeautyGitCDN>https://gitee.com/liesauer/HostFXRPatcher</BeautyGitCDN> -->
<!-- <BeautyGitTree>master</BeautyGitTree> -->
</PropertyGroup>
<ItemGroup>
<PackageReferenceInclude="nulastudio.NetBeauty"Version="2.1.4.4" />
</ItemGroup>
</Project>


執行 dotnet build 或者 dotnet publish , 最佳化輸出操作會自動完成。

3.2. 如果套用已經釋出?

如果你的應用程式已經釋出,可以這樣使用(站長沒試過,這可以做為釋出後補償措施):

Usage:
nbeauty2 [--loglevel=(Error|Detail|Info)] [--srmode] [--enabledebug] [--usepatch] [--hiddens=hiddenFiles] [--noruntimeinfo] [--roll-forward=<rollForward>] [--apphostentry=<appHostEntry>] [--apphostdir=<appHostDir>] <beautyDir> [<libsDir> [<excludes>]]

例如:

ncbeauty2 --usepatch --loglevel Detail --hiddens "hostfxr;hostpolicy;*.deps.json;*.runtimeconfig*.json" /path/to/publishDir libraries "dll1.dll;lib*;..."

3.3. 配置為.NET Core全域工具

dotnet tool install --global nulastudio.nbeauty

安裝後在程式釋出時會自動套用。

unset unset 4. 各種計畫使用範例 unset unset

複制倉庫( https://github.com/nulastudio/NetBeauty2 [12] ),裏面有各種模版計畫使用範例:

測試工程名 說明
WPFTest WPF計畫(Winform類似),預設.NET 5
WebAppTest RazorPages計畫,預設.NET 6
NetFxTest .NET Framework WPF計畫(.NET 4.x,Winform類似)
ChromelyTest 參照了Chromely的.NET計畫
AvaloniaTest Avalonia UI計畫,預設.NET 5

小知識1

Chromely NuGet包是一個用於建立跨平台桌面套用的庫,它提供了一個基於Chromium的瀏覽器控制項。透過Chromely,開發者可以使用Web技術(如HTML、CSS和JavaScript)來構建桌面套用的使用者介面,同時保留對本地系統資源的存取。

Chromely NuGet包提供了一套完整的API和工具,使得開發者可以輕松地將Web應用程式轉換為桌面應用程式,而無需進行大量的程式碼重寫或修改。它還支持各種外掛程式和擴充套件,以便開發者可以根據需要添加額外的功能或客製現有的功能。

此外,Chromely還支持多種程式語言和框架,如C#、.NET Core、ASP.NET Core等,這使得開發者可以選擇他們最熟悉的技術棧來構建應用程式。

小知識2

Avalonia UI是一個跨平台的.NET UI框架,它允許開發者使用XAML和C#語言建立可在多個平台上執行的應用程式,包括Windows、Linux、macOS、iOS、Android以及WebAssembly。Avalonia UI旨在幫助開發者構建漂亮、現代的圖形化使用者介面(GUI)。它相容所有支持.NET Standard 2.0的平台,使開發者能夠從單個程式碼庫建立適用於多個作業系統的原生應用程式。透過使用Avalonia UI,開發者可以充分利用.NET生態系的強大功能,同時實作跨平台相容性,降低開發成本並提高開發效率。

推薦開源控制項庫: irihitech/Semi.Avalonia [13] irihitech/Ursa.Avalonia [14]

unset unset 5. 總結 unset unset

林德熙大佬分享過類似的包 NuGet Gallery | dotnetCampus.PublishFolderCleaner 3.11.1 [15] ,但該庫說明只在Windows釋出支持,大家可以對比使用,原文連結: PublishFolderCleaner 讓你的 dotnet 套用釋出資料夾更加整潔 - lindexi - 部落格園 (cnblogs.com) [16] ,再次給出本文介紹庫NetBeauty2開源地址:

計畫倉庫地址: https://github.com/nulastudio/NetBeauty2 [17]

參考:

  • nulastudio/NetBeauty2: Move a .NET Framework/.NET Core app runtime components and dependencies into a sub-directory and make it beauty. (github.com) [18]

  • 路遙工具箱 .NET 6.0 獨立部署時最佳化目錄結構-碼農很忙 (coderbusy.com) [19]

  • PublishFolderCleaner 讓你的 dotnet 套用釋出資料夾更加整潔 - lindexi - 部落格園 (cnblogs.com) [20]

  • 參考資料

    [1]

    https://github.com/nulastudio/NetBeauty2: https://github.com/nulastudio/NetBeauty2

    [2]

    --hiddens : https://github.com/nulastudio/NetBeauty2#use-the-binary-application-if-your-project-has-already-been-published

    [3]

    NetBeauty 2: https://github.com/nulastudio/NetBeauty2

    [4]

    NetCoreBeauty: https://github.com/nulastudio/NetBeauty2/tree/v1

    [5]

    STARTUP_HOOKS : https://github.com/dotnet/runtime/blob/main/docs/design/features/host-startup-hook.md

    [6]

    AssemblyLoadContext.Resolving : https://docs.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext.resolving?view=netcore-3.0

    [7]

    AssemblyLoadContext.ResolvingUnmanagedDll : https://docs.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext.resolvingunmanageddll?view=netcore-3.0

    [8]

    patched libhostfxr : https://github.com/nulastudio/HostFXRPatcher

    [9]

    additionalProbingPaths : https://github.com/dotnet/toolset/blob/master/Documentation/specs/runtime-configuration-file.md#runtimeoptions-p-runtimeconfigjson

    [10]

    patched libhostfxr : https://github.com/nulastudio/HostFXRPatcher

    [11]

    additionalProbingPaths : https://github.com/dotnet/toolset/blob/master/Documentation/specs/runtime-configuration-file.md#runtimeoptions-p-runtimeconfigjson

    [12]

    https://github.com/nulastudio/NetBeauty2: https://github.com/nulastudio/NetBeauty2

    [13]

    irihitech/Semi.Avalonia: https://github.com/irihitech/Semi.Avalonia

    [14]

    irihitech/Ursa.Avalonia: https://github.com/irihitech/Ursa.Avalonia

    [15]

    NuGet Gallery | dotnetCampus.PublishFolderCleaner 3.11.1: https://www.nuget.org/packages/dotnetCampus.PublishFolderCleaner

    [16]

    PublishFolderCleaner 讓你的 dotnet 套用釋出資料夾更加整潔 - lindexi - 部落格園 (cnblogs.com): https://www.cnblogs.com/lindexi/p/15423277.html

    [17]

    https://github.com/nulastudio/NetBeauty2: https://github.com/nulastudio/NetBeauty2

    [18]

    nulastudio/NetBeauty2: Move a .NET Framework/.NET Core app runtime components and dependencies into a sub-directory and make it beauty. (github.com): https://github.com/nulastudio/NetBeauty2

    [19]

    路遙工具箱 .NET 6.0 獨立部署時最佳化目錄結構-碼農很忙 (coderbusy.com): https://www.coderbusy.com/archives/2301.html

    [20]

    PublishFolderCleaner 讓你的 dotnet 套用釋出資料夾更加整潔 - lindexi - 部落格園 (cnblogs.com): https://www.cnblogs.com/lindexi/p/15423277.html