出自 Arch Linux 中文维基

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

安裝

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

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

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

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

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

通過腳本安裝

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

以下示例會將 LTS(長期支持)版本安裝到默認位置,即 $HOME/.dotnet 下:

# ./dotnet-install.sh --channel LTS

以下是指定特定目錄安裝的命令,在這個示例中 STS(標準期限支持)渠道的最新版將會被安裝到 /usr/share/dotnet 下:

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

你可以通過添加 -ProxyAddress 參數來指定代理,以及通過添加 -Runtime 參數來實現只安裝運行時的目的,具體信息請查閱上面提供的官方文檔連結。

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

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

手動卸載已安裝版本

如果你想卸載以前通過 dotnet-install.sh 腳本安裝的過時版本,並且是在官方提供的卸載工具 .NET Uninstall Tool 還是沒有支持 LInux 的情況下,你可以按照以下步驟完成:

$ 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

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

$ 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 安裝

Some of the AUR dotnet packages are made to be installed alongside each other. Only one host package (dotnet-host-binAUR or dotnet-host) is needed containing the command-line tool and you can install any of the available SDKs and Runtimes (latest packages of all major versions) next to it. List of compatible packages:

安裝 PowerShell Core

You can install PowerShell Core as a "global" tool also [1] [2]

# dotnet tool install --global PowerShell

to update to the current version

# dotnet tool update --global PowerShell

遙測

遙測功能默認被打開,可以通過設置環境變量 DOTNET_CLI_TELEMETRY_OPTOUT=1 關閉遙測。

問題解決

It was not possible to find any compatible framework version

If you get the following error when you try to run a newly created project, you no longer need to set a DOTNET_ROOT variable as described in the solutions of various GitHub issues. Arch's dotnet package (as of 3.1) installs it to the Microsoft recommended location of /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

This is caused because the runtime is shipped as a separate package in Arch. You just need to make sure you have the aspnet-runtime package installed as well.

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

Some of the dotnet SDK tools (for example libman, dotnet-watch etc.) may expect you to have the environment variable DOTNET_ROOT pre-configured. If it is not, an error like this one could be observed: [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.

The workaround is to manually export DOTNET_ROOT in your shell:

~/.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.

This happens after an update. The currently running shell / login session is storing environment variables for the dotnet SDK version different from one installed. Restarting the shell or logging in again should fix this.

無法找到指定的 SDK

Mono 和 Dotnet 的 MSBuild SDK 庫衝突了,請在 shell 中手動設置路徑,將示例中的版本號替換為實際安裝的版本:

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

dotnet command still installed

Installed packages do not uninstall dotnet-host, so uninstall dotnet-host

參閱