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

Are you a .NET Core developer who enjoys working on Windows and Visual Studio, but need to test your application on Linux? Are you a cross-platform developer looking for 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 your inner loop? I have an extension for you! The .NET Core Debugging Extension with WSL 2 - Preview gives you the ability to run and debug .NET Core applications in WSL 2 without leaving Visual Studio.







When should you debug in WSL 2?



For a Linux-centric 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 or with containers using the Container Tools . When production realism is your main concern, you should use one of these. When a simple 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 run profile for Docker and WSL 2 in the same project and choose the one that suits a particular run. 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



Make sure to install WSL 2 and the distribution of your choice before using the extension . After installing the extension, when you open an ASP.NET Core web app or .NET Core console app in Visual Studio, you will see a new launch profile named WSL 2:







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 choosing a 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 are ready to debug in WSL 2. Just start debugging as usual and your application will now run on the default WSL 2 distribution. An easy way to make sure you are running 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 specific distribution



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


Targeting multiple distributions



Going even further, if you're working on an application that needs to run on multiple distributions and you want a quick way to test each one, you might have multiple launch profiles. For example, if you need to test a console application on Debian, Ubuntu 18.04, and Ubuntu 20.04, you can use the following startup 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 launch profiles, you can easily switch between target distributions without leaving the comfortable Visual Studio environment:







Try it today!



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



All Articles