With the raise 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 on 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 take times 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 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.
wsl --setdefault Ubuntu-20.04
Install the VS Extension: .NET Core Debugging with WSL 2 - Preview
Change the Debug target:
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 exemple 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!