Home .NET Debug your .NET Core applications in WSL 2with Visual Studio

Debug your .NET Core applications in WSL 2with Visual Studio

by admin

Are you a .NET Core developer who likes working in Windows and Visual Studio, but needs to test your application in Linux? Are you a cross-platform developer who needs an easy way to test more target environments? Have you already discovered the benefits of WSL 2, but need a way to integrate it into the back-end loop? I have an extension for you! Extension NET Core Debugging with WSL 2 – Preview gives you the ability to run and debug .NET Core applications in WSL 2 without leaving Visual Studio.

When should I debug in WSL 2?

For the Linux-oriented Windows .NET user, WSL 2 is in the right place between production realism and performance. In Visual Studio, you can already debug in a remote Linux environment using the remote debugger ( Remote Debugger ) or with containers using container tools ( Container Tools ). When your main concern is production realism, you should use one of these. When easy and fast inner loop is more important, WSL 2 is a great option.

You don’t have to choose just one! You can have a startup profile for Docker and WSL 2 in the same project and choose the one that suits your particular startup. And once your application is deployed, you can always use the remote debugger to connect to it in case of a problem.

Getting Started with .NET Core Debugging with WSL 2 – Preview

Be sure to install WSL 2 and your chosen distribution After installing the extension, when you open an ASP.NET Core web application or a .NET Core console application in Visual Studio, you will see a new launch profile named WSL 2:
Debug your .NET Core applications in WSL 2with Visual Studio
Selecting this profile will add it to the launchSettings.json file and will look something like this :

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

After selecting the new profile, the extension checks if your WSL 2 distribution is configured to run .NET Core applications and helps you install any missing dependencies. Once all the dependencies are installed, you’re ready to debug in WSL 2. Simply run debugging as usual, and your application will now run in the WSL 2 distribution by default. An easy way to make sure you’re running in Linux is to check the Environment.OSVersion value.

Note. Only Ubuntu and Debian have been tested and supported. Other distributions supported by .NET Core should work, but require manual installation of .NET Core Runtime and Curl.

Using a certain distribution

By default, the WSL 2 startup profile will use the default distribution set in wsl.exe. If you want your startup profile to target a specific distribution, regardless of this default, you can change your startup profile. For example, if you are debugging a web application and want to test it on Ubuntu 20.04, your launch profile would look like 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-20.04"}

Multi-Distribution Orientation

Going even further, if you’re working on an application that needs to run in multiple distributions, and you need a quick way to test each of them, you may have multiple launch profiles. For example, if you need to test a console application in Debian, Ubuntu 18.04 and Ubuntu 20.04, you can use the following launch profiles :

"WSL 2 : Debian": {"commandName": "WSL2", "distributionName": "Debian"}, "WSL 2 : Ubuntu 18.04": {"commandName": "WSL2", "distributionName": "Ubuntu-18.04"}, "WSL 2 : Ubuntu 20.04": {"commandName": "WSL2", "distributionName": "Ubuntu-20.04"}

With these startup profiles, you can easily switch between target distributions without leaving the comfort of your Visual Studio environment:
Debug your .NET Core applications in WSL 2with Visual Studio

Try it today!

So, if you like working in Visual Studio but need to test your application in Linux, go to Visual Studio Marketplace , to install the extension today. Please use the marketplace to ask any questions and leave your feedback, and to let us know how useful this extension is.

You may also like