域名购买后网站搭建,艾辰做网站,wordpress 微博图床,小猫mip网站建设翻译自 haydenb 2020年6月3日的文章《Getting started with cross-platform development using .NET on Ubuntu on WSL》 [1].NET 是一个开源软件框架#xff0c;用于在 Linux、Windows 和 macOS 上构建跨平台应用程序。WSL 上的 Ubuntu [2]允许您同时为 Ubuntu 和 Windows 构… 翻译自 haydenb 2020年6月3日的文章《Getting started with cross-platform development using .NET on Ubuntu on WSL》 [1].NET 是一个开源软件框架用于在 Linux、Windows 和 macOS 上构建跨平台应用程序。WSL 上的 Ubuntu [2]允许您同时为 Ubuntu 和 Windows 构建和测试应用程序。当我们把这些融合在一起时会发生什么呢这篇博客将演示如何在 WSL 上安装 .NET 开发栈并构建一个简单的操作系统感知应用然后在 Linux 和 Windows 上测试它。启用 WSL 1以管理员方式启动 PowerShell 并运行Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
如果您只想安装 WSL 1您可以重启电脑并跳过下一步。Restart-Computer
如果您要安装 WSL 2请不要重启继续下一步操作启用 WSL 2 (Windows 10 2004)想要了解更多关于 Ubuntu on WSL 2 的细节请查看 “Ubuntu on WSL 2 Is Generally Available” [3]。以管理员方式启动 PowerShell 并运行dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
然后重启 Windows 操作系统Restart-Computer
在 WSL 上安装 Ubuntu从 Microsoft Store 中下载 UbuntuUbuntu 20.04 LTS on the Microsoft Store [4]想要了解更多在 WSL 上安装 Ubuntu 的方法请查看 Ubuntu on WSL wiki 页面 [5]。安装 Windows Terminal从 Microsoft Store 中下载 Windows TerminalWindows Terminal on the Microsoft Store [6]也可以从 GitHub 下载 Windows Terminal。运行 WSL 上的 Ubuntu打开 Windows Terminal 并运行ubuntu.exe
当您首次在 WSL 上运行 Ubuntu 时它将安装并提示您创建一个 Linux 用户这个用户是独立于 Windows 用户的。退出并重新打开 Windows Terminal您将会发现 Ubuntu 出现在下拉菜单中您可以在 settings.json 中设置 Windows Terminal将 Ubuntu 设置为默认项。更新 WSL 上的 Ubuntu您应该定期检查更新并在 WSL 上的 Ubuntu 中运行升级。我们用 apt (Ubuntu 包管理器)来实现。要检查更新请运行sudo apt update
要获得升级请运行sudo apt upgrade
您可以通过用 将它们连接在同一行并添加 -y 标签自动更新并应用可用的升级sudo apt update sudo apt upgrade -y
添加微软的 .NET 资源库和签名密钥我们需要为 apt 添加微软的 .NET 资源库和签名密钥。我们将从微软下载并安装一个包来完成这项工作。请确保您正在为您的 Ubuntu 版本安装正确的资源库。您可以使用下面的命令检查 Ubuntu 的当前版本cat /etc/os-release
下面的示例使用 Ubuntu 20.04来自 Canonical 的最新 LTS 发行版。如果您仍在使用 Ubuntu 16.04、18.04 或 19.10您可以在微软文档 [7]中找到相应的资源库。想要了解更多关于 LTS 和中间版本之间的区别我们有一个发布周期页面 [8]。为 20.04 版本下载微软的资源库和密钥包wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
使用 dpkg -i 手动安装微软资源包sudo dpkg -i packages-microsoft-prod.deb
现在当你更新 apt 时你会看到微软资源库已检查升级了安装 .NET SDK使用 apt 从微软资源库安装 .NET 和相关依赖项sudo apt-get install dotnet-sdk-3.1 -y
新建工作区创建一个新的工作目录并打开该目录mkdir dotnetproject
cd dotnetproject/
新建一个 .NET 项目使用 dotnet new 创建一个新的 .NET 控制台项目这会创建一个名为 Program.cs 的文件和其他一些必要的文件夹和文件dotnet new console
探索我们的 .NET 应用列出您的新 .NET 项目中的文件ls
查看 Program.cs 的内容cat Program.cs
运行示例程序dotnet run
自定义我们的 .NET 应用在您最喜欢的编辑器中打开 Program.csvi、nano、emacs 或者有 remote WSL 扩展的 VS Code在这里我们使用 WSL 上的 Ubuntu 中包含的 nanonano Program.cs
首先我们添加 Interop services 命名空间using System.Runtime.InteropServices;
然后把Console.WriteLine(Hello World!);
替换成Console.WriteLine($Hello {System.Environment.GetEnvironmentVariable(USER)});if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{Console.WriteLine(Were on Linux!);
}if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{Console.WriteLine(Were on Windows!);
}Console.WriteLine(Version {0}, Environment.OSVersion.Version);
这段代码也可以在这里 [9]找到。这个应用程序告诉我们当前的用户检查是在 Windows 还是 Linux 上然后给出 OS 内核版本。退出并保存然后运行dotnet run
让我们的 .NET 应用程序跨平台我们需要更新 .NET 项目文件 dotnetproject.csproj告诉 .NET 同时为 Linux 和 Windows 平台构建。在我们的编辑器中打开 dotnetproject.csproj 并添加PropertyGroupRuntimeIdentifierswin10-x64;linux-x64/RuntimeIdentifiers
/PropertyGroup
这将引导 .NET 同时为 Windows 10 x64 和 Linux x64 构建自包含的二进制文件。构建我们的跨平台应用程序当我们配置好项目后构建 .NET 应用程序变得如此简单dotnet publish -r win10-x64
dotnet publish -r linux-x64
可以在项目的 /bin/ 文件夹中找到每个平台的自包含二进制文件及其所有必需的库ls bin/Debug/netcoreapp3.1/
测试 Linux 版本您可以直接运行 Linux 二进制文件如下所示./bin/Debug/netcoreapp3.1/linux-x64/publish/dotnetproject
测试 Windows 版本要运行 Windows 版本请将其复制到 Windows 文件系统中cp -r ~/dotnetproject/bin/Debug/netcoreapp3.1/win10-x64/publish /mnt/c/Users/Hayden/OneDrive/Desktop/
译者注此处的 /mnt/ 为 Ubuntu 系统中看到的 Windows 文件系统的根目录/mnt/c/ 即为 Windows 系统中的 C 盘。然后运行/mnt/c/Users/Hayden/OneDrive/Desktop/publish/dotnetproject.exe
至此我们已经为 Linux 和 Windows 构建并运行了相同的应用程序。我们可以使用 WSL 同时测试它们。相关链接https://ubuntu.com/blog/creating-cross-platform-applications-with-net-on-ubuntu-on-wsl Getting started with cross-platform development using .NET on Ubuntu on WSL ↩︎https://ubuntu.com/wsl Ubuntu on WSL ↩︎https://ubuntu.com/blog/ubuntu-on-wsl-2-is-generally-available Ubuntu on WSL 2 Is Generally Available ↩︎https://www.microsoft.com/store/productId/9N6SVWS3RX71 Ubuntu 20.04 LTS on the Microsoft Store ↩︎https://wiki.ubuntu.com/WSL Ubuntu on WSL wiki ↩︎https://www.microsoft.com/store/productId/9N0DX20HK701 Windows Terminal on the Microsoft Store ↩︎https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu ↩︎https://ubuntu.com/about/release-cycle ↩︎https://pastebin.ubuntu.com/p/swbPxXXSKD/ ↩︎作者 haydenb 译者 技术译民 出品 技术译站https://ITTranslator.cn/END