Running and debugging a .NET Core application in WSL 2 from Visual Studio

 
 
  • Gérald Barré

With the rise of microservices, it is more and more common to develop .NET applications on Windows and deploy them on Linux. There are lots of differences between Windows and Linux, so you should better debug on Linux directly instead of Windows to avoid errors in production. In Visual Studio, you can use Docker to debug your application on Linux. If you don't use Docker to deploy your application, setting up a Docker file takes time and knowledge. You could also use SSH to debug an application running on Linux, but the development workflow is not convenient.

The new Visual Studio extension allows us to quickly run and debug a .NET application on WSL 2 from Visual Studio. WSL 2 includes a Linux kernel, so this is very similar to executing the app on an actual Linux machine. Also, WSL 2 is convenient as it shares the same file system as Windows, so you can easily switch between debugging on Windows and debugging on WSL.

  1. Install WSL (documentation) and be sure the default WSL distribution is supported. Currently, Ubuntu and Debian are supported.

    Shell
    wsl --list
    Shell
    wsl --setdefault Ubuntu-20.04
  2. Install the VS Extension: .NET Core Debugging with WSL 2 - Preview

  3. Change the Debug target:

  4. Install .NET and vsdbg in the WSL instance if needed using the button in the Visual Studio gold bar and the error window:

You can validate it starts on WSL by adding Environment.OSVersion. Here's an example of a web application:

Note that this extension doesn't support running tests in WSL 2. It only supports running and debugging the application.

Do you have a question or a suggestion about this post? Contact me!

Follow me:
Enjoy this blog?Buy Me A Coffee💖 Sponsor on GitHub