跳转到内容
来自 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

参阅