Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

栏目: IT技术 · 发布时间: 5年前

内容简介:.NET is an open source software framework for building cross-platform applications on Linux, Windows, and macOS.Ubuntu on WSL allows you to build and test applications for Ubuntu and Windows simultaneously. What happens when we mix these together? This blo

.NET is an open source software framework for building cross-platform applications on Linux, Windows, and macOS.Ubuntu on WSL allows you to build and test applications for Ubuntu and Windows simultaneously. What happens when we mix these together? This blog will demonstrate how to install a .NET development stack on WSL, build a simple OS-aware application, and then test it on both Linux and Windows.

Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Enable WSL 1

In PowerShell as Administrator run:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

and then restart Windows:

Restart-Computer

Enable WSL 2 (Windows 10 2004+)

See “ Ubuntu on WSL 2 Is Generally Available ” for more details on Ubuntu on WSL 2.

In PowerShell as Administrator run:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

and then restart Windows:

Restart-Computer

Install Ubuntu on WSL

Download Ubuntu from the Microsoft Store :

Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

For more ways to install Ubuntu on WSL, see the Ubuntu on WSL wiki page .

Install Windows Terminal

Download the Windows Terminal from the Microsoft Store :

Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Windows Terminal can also be downloaded from GitHub .

Run Ubuntu on WSL

Open Windows Terminal in PowerShell and run:

ubuntu.exe

When you run Ubuntu on WSL for the first time it will install and then you will create a Linux user. This is separate from your Windows user.

Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Exit and re-open Windows Terminal and you will find Ubuntu on the drop-down:

Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

You can set Ubuntu as the default and configure Windows Terminal in settings.json .

Update Ubuntu on WSL

You should periodically check for updates and run upgrades on Ubuntu on WSL. We do this with apt, the Ubuntu package manager.

To check for updates run:

sudo apt update
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

To get upgrades run:

sudo apt upgrade
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

You can automatically apply any available upgrades by adding the -y flag:

sudo apt upgrade -y

Add Microsoft’s .NET repository and signing key

We need to add Microsoft’s .NET repository and signing key to apt. We will download and install a package from Microsoft that will do this.

Download the Microsoft repository and key package for 20.04:

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Install the Microsoft repo package manually using dpg -i:

sudo dpkg -i packages-microsoft-prod.deb
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Now when you update apt you will see the Microsoft repository is checked for upgrades:

Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Install .NET SDK

Install the .NET and related dependencies using apt from the Microsoft repository:

sudo apt-get install dotnet-sdk-3.1 -y
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Create a workspace

Create a new directory for to work in and change to that directory:

mkdir dotnetproject
cd dotnetproject/
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Create a new .NET project

Create a new .NET console project using dotnet new. This will create a file called Program.cs and other necessary folders and files:

dotnet new console
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Explore our .NET app

List the files in your new .NET project:

ls

View the contents of Program.cs:

cat Program.cs
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Run the sample program:

dotnet run
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Customize our .NET app

Open Program.cs in your favorite editor: vi, nano, emacs, or Code with the remote WSL extension:

Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

For our purposes, we will use nano, which is included with Ubuntu on WSL:

nano Program.cs
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

To our header add the Interop services namespace :

using System.Runtime.InteropServices;

Then replace:

Console.WriteLine("Hello World!");

With:

Console.WriteLine($"Hello {System.Environment.GetEnvironmentVariable("USER")}");

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
 {
  Console.WriteLine("We're on Linux!");
 }

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
 {
  Console.WriteLine("We're on Windows!");
 }

Console.WriteLine("Version {0}", Environment.OSVersion.Version);
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

This application tells us our current user, checks if we are on Windows or Linux, and then gives the OS kernel version.

Exit and save and run with:

dotnet run
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Make our .NET application cross-platform

We need to update our .NET project file, dotnetproject.csproj, to tell .NET to build for both Linux and Windows.

Open dotnetproject.csproj in our editor and add:

<PropertyGroup>
    <RuntimeIdentifiers>win10-x64;linux-x64</RuntimeIdentifiers>
 </PropertyGroup>

This directs .NET to build self-contained binaries for both Windows 10 x64 and Linux x64.

Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Build our cross-platform application

Once our project is properly configured, building our .NET application is as simple as:

dotnet publish -r win10-x64
dotnet publish -r linux-x64
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Self-contained binaries for each platform with all required libraries can be found in the project’s /bin/ folder:

ls bin/Debug/netcoreapp3.1/
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Test Linux build

You can run your Linux binary direct as follows:

./bin/Debug/netcoreapp3.1/linux-x64/publish/dotnetproject
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

Test Windows build

To run the Windows build, copy it to your Windows file system:

cp -r ~/dotnetproject/bin/Debug/netcoreapp3.1/win10-x64/publish /mnt/c/Users/Hayden/OneDrive/Desktop/

Then run:

/mnt/c/Users/Hayden/OneDrive/Desktop/publish/dotnetproject.exe
Ubuntu Blog: Creating cross-platform applications with .NET on Ubuntu on WSL

We have built and run the same application for both Linux and Windows. We can test them both simultaneously using WSL.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

深入浅出强化学习:原理入门

深入浅出强化学习:原理入门

郭宪、方勇纯 / 电子工业出版社 / 2018-1 / 79

《深入浅出强化学习:原理入门》用通俗易懂的语言深入浅出地介绍了强化学习的基本原理,覆盖了传统的强化学习基本方法和当前炙手可热的深度强化学习方法。开篇从最基本的马尔科夫决策过程入手,将强化学习问题纳入到严谨的数学框架中,接着阐述了解决此类问题最基本的方法——动态规划方法,并从中总结出解决强化学习问题的基本思路:交互迭代策略评估和策略改善。基于这个思路,分别介绍了基于值函数的强化学习方法和基于直接策略......一起来看看 《深入浅出强化学习:原理入门》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具