.NET

来自 ArchWiki
(重定向自 .NET Core

.NET(之前名为 .NET Core)是由 Microsoft 开发的 FOSS 软件框架,用于 C#、Visual Basic 和 F#。它被设计为跨平台的、模块化的,并且适用于现代应用程序,这与它的前身 .NET Framework 不同。

.NET 源代码可在 GitHub 上的 dotnet/dotnet 找到。

安装

如果您只想运行 .NET 托管应用程序,请安装 dotnet-runtime 软件包。

要使用 .NET 构建应用程序,请同时安装 dotnet-sdk

最后,要使用 ASP.NET Core 构建动态网站、应用程序和服务,请安装 aspnet-runtime

Microsoft 建议使用 Visual Studio Code,他们基于 Electron 的 IDE,来构建和调试 .NET 应用程序。

提示:~/.dotnet/tools 添加到 PATH,否则 dotnet 工具将无法从 shell 工作。

要使用 .NET 6.0,请在上述软件包后添加“-6.0”后缀,例如 dotnet-runtime-6.0dotnet-sdk-6.0aspnet-runtime-6.0

版本差异

.NET SDK 以多种版本方案发布。只有 SDK 版本 1xx 可以从源代码构建,并且将在官方存储库中提供。如果您需要任何其他版本,您将需要 AUR 中的 *-bin 软件包之一。

手动安装多个版本

您可以使用 .NET 基金会提供的 dotnet-install.sh 脚本并排安装多个版本的 .NET SDK 或运行时。您可以在此处找到该脚本的文档。

例如,此命令会将“STS”(标准期限支持)通道中找到的最新版本安装到 /usr/share/dotnet

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

您可能希望首先使用 -Dryrun 标志模拟安装。

安装完成后,您可以验证可用的 SDK

$ 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 卸载工具 仍然不支持 Linux,因此必须手动完成卸载。

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

卸载 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 和共享软件包,根据版本,可能需要额外删除它们。

完整卸载 .NET 版本(sdk、host、shared)

$ 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 软件包旨在彼此并排安装。只需要一个 host 软件包 (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 运行时组件在任何情况下都不会收集遥测数据。

根据 Red Hat 对 .NET SDK 的更改,社区构建(包括 Arch 中的,从 .NET 7 开始)不收集遥测数据。

可以通过设置环境变量 DOTNET_CLI_TELEMETRY_OPTOUT=1 来禁用遥测。

Tab 补全

所有使用 System.CommandLine.Parser 解析其参数的 dotnet 程序都具有自动完成支持。启用它只需要在您的 .bashrc / .zshrc 文件中添加几行代码,如他们的文档中所述。有关独立二进制文件的说明,请参见此处

故障排除

找不到任何兼容的框架版本

如果您在尝试运行新创建的项目时收到以下错误,则不再需要像各种 GitHub 问题的解决方案中所述那样设置 DOTNET_ROOT 变量。Arch 的 dotnet 软件包(截至 3.1)将其安装到 Microsoft 建议的位置 /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 软件包。

“找不到所需的库 libhostfxr.so” 错误

一些 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.

解决方法是在您的 shell 中手动导出 DOTNET_ROOT

~/.bashrc
export DOTNET_ROOT=/opt/dotnet

Error MSB4019: 未找到导入的项目“/usr/share/dotnet/sdk/.../Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.Common.targets”。请确认 Import 声明中的表达式 ... 是否正确,以及磁盘上是否存在该文件。

这在更新后发生。当前运行的 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

参见