Debug .Net Core apps using Linux Kernal in Windows OS

C# CONCEPTS

Run .Net Core Apps in WSL2(Windows subsystem for Linux) using Visual Studio in Windows.

What is WSL 2?

The Windows Subsystem for Linux(WSL 2) lets developers operate on a Linux environment without the burden of a conventional virtual machine or the dual operating systems.

Here is the link to install the WSL 2 extension for Visual Studio

https://marketplace.visualstudio.com/items?itemName=ms-azuretools.Dot-Net-Core-Debugging-With-Wsl2

After the extension is successfully installed, open an ASP.NET Core web app or .NET Core console app using the Visual Studio, a new Launch Profile named WSL 2 will be visible as shown below.

The relevant launchsetting.json will look similar to this.

"WSL 2": {
"commandName": "WSL2",
"launchBrowser": true,
"launchUrl": "https://localhost:5001",
"environmentVariables": {
"ASPNETCORE_URLS": "https://localhost:5001;http://localhost:5000",
"ASPNETCORE_ENVIRONMENT": "Development"
},
"distributionName": ""
}

Now start Debugging as usual, and now .Net Core app is running on default WSL 2 distribution.

There are two ways to target distributions with WSL2.

  • SIngle/Specific Distribution
  • Multiple Distribution

Targeting specific distribution

By default, the WSL 2 launch profile will use the default distribution asset in wsl.exe.

If required to launch profile to a particular distribution, alter your launch profile. For example, debugging a web app on Ubuntu xx.xx, the launch profile would look similar to this:

"WSL 2": {
"commandName": "WSL2",
"launchBrowser": true,
"launchUrl": "https://localhost:5001",
"environmentVariables": {
"ASPNETCORE_URLS": "https://localhost:5001;http://localhost:5000",
"ASPNETCORE_ENVIRONMENT": "Development"
},
"distributionName": "Ubuntu-xx.xx"
}

Targeting multiple distributions

If working on an application that requires to run in multiple distributions and test on each of them, you can have various launch profiles.

For instance, if you need to test your console app on Debian, Ubuntu xx.xx, and Ubuntu yy.yy, you could use the following launch profiles:

"WSL 2 : Ubuntu xx.xx": {
"commandName": "WSL2",
"distributionName": "Ubuntu-xx.xx"
},
"WSL 2 : Ubuntu yy.yy": {
"commandName": "WSL2",
"distributionName": "Ubuntu-yy.yy"
}

With these launch profiles, one can quickly shift back and forth between target distributions.


I hope you found this article helpful, please let me know your thoughts.

Leave a comment