跳至內容

.NET

出自 Arch Linux 中文维基

.NET(舊稱 .NET Core)是 Microsoft 開發的開源軟體框架,支持 C#, Visual Basic 和 F#。和之前的 .NET Framework 相比,它支持跨平台開發,設計上更加模塊化,面向現代程序開發。

.NET 源碼位於存放在 Github 上的 dotnet/dotnet

安裝

[編輯 | 編輯原始碼]

如果要運行 .NET 管理的程序,請安裝 dotnet-runtime

要使用 .NET 編譯程序,還需要安裝 dotnet-sdk

要使用 ASP.NET Core 建立動態網站,應用和服務,安裝 aspnet-runtime.

微軟推薦使用 Visual Studio Code 編譯和調試 .NET 程序,它是微軟開發的基於 Electron 的開源 IDE。

提示:~/.dotnet/tools 加入 PATH,否則 dotnet 工具無法在 shell 中啟動。

如果要使用 .NET 6.0,將上述包加上「-6.0」後綴即可(例如 dotnet-runtime-6.0dotnet-sdk-6.0aspnet-runtime-6.0

版本區別

[編輯 | 編輯原始碼]

.NET SDK 有多個版本,只有 1xx 版本可從源碼構建,並在官方倉庫中提供。如果需要其它版本,可以在 AUR 中查找對應的 *-bin 包。

手動安裝多個版本

[編輯 | 編輯原始碼]

你可以通過執行 .NET 官方提供的 dotnet-install.sh 腳本來同時安裝 .NET SDK 或 .NET 運行時的多個版本。另外,你可以在此連結中找到關於這個腳本的說明。

例如,該命令將安裝「STS」(標準期限支持)頻道的最新版本到 /usr/share/dotnet

# ./dotnet-install.sh --install-dir /usr/share/dotnet -channel STS -version latest

你可以先使用 -Dryrun 來模擬安裝。

腳本執行完畢後,你可以通過以下命令來驗證是否安裝成功:

$ dotnet --list-sdks
2.2.108 [/usr/share/dotnet/sdk]
3.0.103 [/usr/share/dotnet/sdk]
$ dotnet --version
3.0.103

手動卸載已安裝版本

[編輯 | 編輯原始碼]

官方提供的卸載工具 .NET Uninstall Tool 尚無 Linux 支持,如果你想卸載以前通過 dotnet-install.sh 腳本安裝的過時版本,需要手動操作。

$ dotnet --list-sdks
5.0.100 [/usr/share/dotnet/sdk]
5.0.102 [/usr/share/dotnet/sdk]

解除特定版本的安裝:

$ SDK_VERSION="5.0.100"
$ DOTNET_UNINSTALL_PATH="/usr/share/dotnet"
# rm -rf $DOTNET_UNINSTALL_PATH/sdk/$SDK_VERSION

dotnet-install.sh 同時會安裝 dotnet host 及共享包,根據版本不同可能需要單獨進行移除。

你可能會安裝了一些額外的依賴,通過下面的命令移除他們:

$ SDK_VERSION="5.0.100"
$ DOTNET_VERSION="5.0.0"
$ DOTNET_UNINSTALL_PATH="/usr/share/dotnet"
# rm -rf $DOTNET_UNINSTALL_PATH/sdk/$SDK_VERSION
# rm -rf $DOTNET_UNINSTALL_PATH/shared/Microsoft.NETCore.App/$DOTNET_VERSION
# rm -rf $DOTNET_UNINSTALL_PATH/shared/Microsoft.AspNetCore.All/$DOTNET_VERSION
# rm -rf $DOTNET_UNINSTALL_PATH/shared/Microsoft.AspNetCore.App/$DOTNET_VERSION
# rm -rf $DOTNET_UNINSTALL_PATH/host/fxr/$DOTNET_VERSION

通過 AUR 安裝多個版本

[編輯 | 編輯原始碼]

有些 AUR dotnet 包可以被同時安裝。只需要安裝 (dotnet-host-binAURdotnet-host) 獲取命令行工具,就可以安裝任意大版本的最新 SDK 和運行時。以下為兼容的包:

安裝 PowerShell Core

[編輯 | 編輯原始碼]

你可以將 PowerShell Core 安裝為「全局」工具 [1] [2]

# dotnet tool install --global PowerShell

按如下方法將其升級到最新版本

# dotnet tool update --global PowerShell

另請參考 PowerShell

遙測

[編輯 | 編輯原始碼]

Microsoft 構建的 .NET SDK 默認啟用遙測。AUR .NET SDK 包(*-bin 變體)基於 Microsoft 構建的 .NET。.NET 運行時組件在任何情況下都不收集遙測信息。

社區構建版本(包括 Arch;從 .NET 7 開始)不收集遙測數據,這是由 Red Hat 對 .NET SDK 做出的更改。

可以通過設置環境變量 DOTNET_CLI_TELEMETRY_OPTOUT=1 來關閉遙測。

Tab 自動補全

[編輯 | 編輯原始碼]

所有使用 System.CommandLine.Parser 解析命令行選項的 dotnet 程序都支持自動補全。要啟用該功能,需按照文檔.bashrc / .zshrc 中添加數行內容。 對於獨立的二進制文件,需參考這裡

故障排除

[編輯 | 編輯原始碼]

It was not possible to find any compatible framework version

[編輯 | 編輯原始碼]

如果在運行新創建的項目時出現了如下報錯,不需要按各種 GitHub issue 中的描述設置 DOTNET_ROOT 變量,Arch 提供的 dotnet 包(從 3.1 版本開始)會安裝到微軟建議的 /usr/share/dotnet 位置。

$ dotnet run
It was not possible to find any compatible framework version
The framework 'Microsoft.AspNetCore.App', version '3.1.0' was not found.
  - No frameworks were found.

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.AspNetCore.App&framework_version=3.1.0&arch=x64&rid=arch-x64

該問題的產生原因是 Arch 將運行時拆分到了單獨的軟體包,你需要確保同時安裝了 aspnet-runtime

"the required library libhostfxr.so could not be found"

[編輯 | 編輯原始碼]

有些 dotnet SDK 工具(如 libmandotnet-watch 等)可能會要求配置 DOTNET_ROOT 環境變量。如果沒有配置該變量,就會出現如下報錯:[3]

A fatal error occurred, the required library libhostfxr.so could not be found.
If this is a self-contained application, that library should exist in [/home/my_user/.dotnet/tools/.store/microsoft.web.librarymanager.cli/1.0.172/microsoft.web.librarymanager.cli/1.0.172/tools/netcoreapp2.1/any/].
If this is a framework-dependent application, install the runtime in the default location [/usr/share/dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location.

解決方案是手動在終端環境導出 DOTNET_ROOT

~/.bashrc
export DOTNET_ROOT=/opt/dotnet

Error MSB4019: The imported project "/usr/share/dotnet/sdk/.../Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.Common.targets" was not found. Confirm that the expression in the Import declaration ... is correct, and that the file exists on disk.

[編輯 | 編輯原始碼]

這是由更新導致的,當前的 shell 或登錄會話中環境變量儲存的 dotnet SDK 版本與已安裝版本不同。重新打開 shell 或重新登錄可以解決該問題。

無法找到指定的 SDK

[編輯 | 編輯原始碼]

這被認為是 Mono 和 MSBuild SDK 庫及 dotnet 庫衝突了。要修正該問題,需參考如下方式手動在 shell 中設置路徑(按需將示例中的版本號替換為實際安裝的版本):

~/.bashrc
export MSBuildSDKsPath=$( echo /usr/share/dotnet/sdk/3.*/Sdks );

仍已安裝 dotnet 命令

[編輯 | 編輯原始碼]

已安裝的包不會卸載 dotnet-host,需要手動卸載 dotnet-host

參閱

[編輯 | 編輯原始碼]