Getting started with cross-platform development using .NET on WSL Ubuntu

Translated from haydenb's article "Getting started with cross-platform development using .NET on Ubuntu on WSL" on June 3, 2020 1

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

Enable WSL 1

Start PowerShell as an administrator and run:

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Enable WSL 1

If you only want to install WSL 1, you can restart your computer and skip the next step.

Restart-Computer

If you want to install WSL 2, please do not restart, continue to the next step:

Enable WSL 2 (Windows 10 2004+)

For more details about Ubuntu on WSL 2, please check " Ubuntu on WSL 2 Is Generally Available " 3 .

Start PowerShell as an administrator and run:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Enable WSL 2

Then restart the Windows operating system:

Restart-Computer

Install Ubuntu on WSL

Download Ubuntu from the Microsoft Store:

Ubuntu 20.04 LTS on the Microsoft Store 4

Ubuntu 20.04 LTS on the Microsoft Store

To learn more about how to install Ubuntu on WSL , please check Ubuntu on WSL wiki page 5 .

Install Windows Terminal

Download Windows Terminal from the Microsoft Store:

Windows Terminal on the Microsoft Store 6

Windows Terminal on the Microsoft Store

You can also download Windows Terminal from GitHub .

Run Ubuntu on WSL

Open Windows Terminal and run:

ubuntu.exe

When you run Ubuntu on WSL for the first time, it will install and prompt you to create a Linux user, which is independent of Windows users.

Run Ubuntu on WSL

Exit and reopen Windows Terminal, you will find Ubuntu appears in the drop-down menu:

Ubuntu on the drop-down

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

Update Ubuntu on WSL

You should check for updates regularly and run the upgrade in Ubuntu on WSL. We use apt (Ubuntu package manager) to achieve.

To check for updates, run:

sudo apt update

sudo apt update

To get the upgrade, run:

sudo apt upgrade

sudo apt upgrade

You can use &&them to connect in the same row and add a -ylabel, automatic updates and application upgrades available:

sudo apt update && 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 to do this.

Please make sure you are installing the correct repository for your version of Ubuntu. You can check the current version of Ubuntu with the following command:

cat /etc/os-release

The example below uses Ubuntu 20.04, the latest LTS release from Canonical. If you are still using Ubuntu 16.04, 18.04 or 19.10, you can find the corresponding resource library in Microsoft Document 7 . To learn more about the difference between LTS and intermediate versions, we have a release cycle page 8 .

Download Microsoft's resource library and key package for version 20.04:

wget https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb

Download the Microsoft repository and key package

Use dpkg -i to manually install the Microsoft Resource Pack:

sudo dpkg -i packages-microsoft-prod.deb

Install the Microsoft repo package

Now when you update apt, you will see that the Microsoft Resource Library has been checked and upgraded:

apt update

Install .NET SDK

Use apt to install .NET and related dependencies from the Microsoft repository:

sudo apt-get install dotnet-sdk-3.1 -y

Install the .NET SDK

New workspace

Create a new working directory and open it:

mkdir dotnetproject
cd dotnetproject/

Create a workspace

Create a new .NET project

Use dotnet newto create a new .NET console project, which will create a named Program.csfile and some other necessary folders and files:

dotnet new console

Create a new .NET project

Explore our .NET applications

List the files in your new .NET project:

ls

Check Program.csthe contents:

cat Program.cs

Explore our .NET app

Run the sample program:

dotnet run

Run the sample program

Customize our .NET application

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

Code with the remote WSL extension

Here, we use nano included in Ubuntu on WSL:

nano Program.cs

nano Program.cs

First, we add the Interop services namespace :

using System.Runtime.InteropServices;

Then put:

Console.WriteLine("Hello World!");

Replace 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);

replace code

This code can also be found here 9 .

This application tells us: the current user, check whether it is on Windows or Linux, and then give the OS kernel version.

Exit and save, then run:

dotnet run

Exit and save and run

Make our .NET application cross-platform

We need to update the .NET project file dotnetproject.csprojto tell .NET to build for both Linux and Windows platforms.

Open our editor dotnetproject.csprojand add:

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

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

Make our .NET application cross-platform

Build our cross-platform application

When we have configured the project, building a .NET application becomes so simple:

dotnet publish -r win10-x64
dotnet publish -r linux-x64

dotnet publish

The project can be /bin/found on each platform binary file folder and all of its necessary libraries self-contained:

ls bin/Debug/netcoreapp3.1/

ls bin/Debug/netcoreapp3.1/

Test the Linux version

You can directly run the Linux binary file as follows:

./bin/Debug/netcoreapp3.1/linux-x64/publish/dotnetproject

Test Linux build

Test the Windows version

To run the Windows version, copy it to the Windows file system:

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

Translator's Note:
Here's /mnt/root directory for the Ubuntu system, see the Windows file system, /mnt/c/namely Windows system C drive.

Then run:

/mnt/c/Users/Hayden/OneDrive/Desktop/publish/dotnetproject.exe

Test Windows build

So far, we have built and run the same application for Linux and Windows. We can use WSL to test them at the same time.


Author: haydenb
Translator: Technical Zemin
Publisher: Technical Verses
links: English text

Public Number: Technical Translation Station


  1. 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 ↩︎

  2. https://ubuntu.com/wsl Ubuntu on WSL ↩︎

  3. https://ubuntu.com/blog/ubuntu-on-wsl-2-is-generally-available Ubuntu on WSL 2 Is Generally Available ↩︎

  4. https://www.microsoft.com/store/productId/9N6SVWS3RX71 Ubuntu 20.04 LTS on the Microsoft Store ↩︎

  5. https://wiki.ubuntu.com/WSL Ubuntu on WSL wiki ↩︎

  6. https://www.microsoft.com/store/productId/9N0DX20HK701 Windows Terminal on the Microsoft Store ↩︎

  7. https://docs.microsoft.com/en-us/dotnet/core/install/linux-ubuntu ↩︎

  8. https://ubuntu.com/about/release-cycle ↩︎

  9. https://pastebin.ubuntu.com/p/swbPxXXSKD/ ↩︎

Guess you like

Origin blog.csdn.net/weixin_47498376/article/details/112051033