.NET

来自 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 中启动。

Install multiple versions manually[编辑 | 编辑源代码]

You can install multiple versions of the .NET SDK or runtime side by side by using the dotnet-install.sh script provided by the .NET Foundation. You can find the documentation of the script here.

For instance, this command would install the latest version found in the "STS"(Standard Term Support) channel in /usr/share/dotnet:

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

You may want to simulate the installation first by using the -Dryrun flag.

Once installed, you can verify the SDKs available:

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

Uninstall manually installed version[编辑 | 编辑源代码]

You may want to remove outdated versions installed with dotnet-install.sh. The automated .NET Uninstall Tool does still not support Linux, so the desinstallation has to be done manually.

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

Desinstallation of the sdk:

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

Using dotnet-install.sh also dotnet host and shared packages are getting installed, which may have to get removed additionally depending on the release.

Complete deinstallation of the .NET Version (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

Install multiple versions via 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:

Install 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

参阅[编辑 | 编辑源代码]