Scott Hanselman

Official Support for Remote Debugging a .NET Core Linux app in WSL2 from Visual Studio on Windows

July 23, '20 Comments [4] Posted in DotNetCore | Linux | VS2019
Sponsored By

I've blogged before about Developing on Docker with the new and improved Visual Studio Container Tools (and WSL2) and also Remote Debugging a .NET Core Linux app in WSL2 from Visual Studio on Windows.

It's the second one that I'm talking about today. You can now run .NET Core console and web apps in WSL2 and debug them directly from Visual Studio 2019!

What do you need?

Here's the experience in Visual Studio 2019 when the extension is installed. It "just works" and it makes it super easy to switch between running on Windows (under IIS or the Kestrel web server or under Kestrel under your default Linux distribution.

WSL 2 in the Visual Studio Debugging Menu

Check this out, you can see that .NET Core, from the Linux/WSL 2 perspective, is loaded out of /usr/share/dotnet/shared but my source remains on my /mnt/d drive (my Windows D:) and debugging Just Works.

image

You'll also notice that we are running on https://localhost:5001 and that localhost and ports from the Windows point of view maps to localhost and points (via a local tunnel that's transparent) to WSL 2.

How does the SSL cert work if WSL 2's Linux Kestrel web server is serving it?

You can see that there's a symbolic link between my WSL ~/.aspnet folder and my local profile in Windows so that this app shares SSL certs and that the same cert is served with Kestrel on Windows and Kestrel on Linux.

scott@IRONHEART:~$ cd .aspnet
scott@IRONHEART:~/.aspnet$ ls
DataProtection-Keys https
scott@IRONHEART:~/.aspnet$ cd https/
scott@IRONHEART:~/.aspnet/https$ ls
hanselminutes.core.pfx
scott@IRONHEART:~/.aspnet/https$ ls -alogF
total 12
drwxr-xr-x 2 4096 Jun 23 17:02 ./
drwxr-xr-x 4 4096 Jun 23 17:02 ../
lrwxrwxrwx 1 71 Jun 23 17:02 hanselminutes.core.pfx
->
/mnt/c/Users/scott/AppData/Roaming/ASP.NET/Https/hanselminutes.core.pfx*
scott@IRONHEART:~/.aspnet/https$

I broke that line up with the symbolic link -> along 3 lines so it wouldn't wrap on this blog.

Now you can run and debug .NET Core apps on Windows and Linux using both VS Code and Visual Studio 2019! I'm using Visual Studio 2019's free Community Edition and it works great. This helps me save money as I've moved my Podcast site to Linux in Azure and it makes my local development better match my cloud reality. Give it a try!


Sponsor: Centralize and search structured application logs to confidently diagnose problems - even faster and easier with Seq 2020.1!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Wednesday, July 29, 2020 2:48:20 PM UTC
Question: The title says "remote debugging a .NET Core Linux app," but the screenshots and most of the article is talking about an ASP.NET web app. Why? ASP.NET Core apps can be debugged on Windows itself; they are sure to work the same on Linux.

Debugging a Linux console app, however, makes sense, because otherwise, one needs a full Linux installation.
Fleet Command
Thursday, July 30, 2020 9:11:36 PM UTC
Fleet Command - it's because a Web App, running in .NET Core under Linux is more complex (it exercises more) than a Console App. In fact, a Web App in .NET Core *is also a console app.* So yes, you can do all of these and both.

The reason you'd want to do this is to make sure that your deployment operating system for your app matches your development environment.
Scott Hanselman
Friday, July 31, 2020 2:06:40 AM UTC
Great article Scott! Just used it to test my asp.net core app that I publish to a Linux app service. Had time zone issues that this helped me resolve.
Ken Brannigan
Friday, August 07, 2020 9:50:05 AM UTC
That's really cool feature - so easy to use and really helps in code developement.
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.