Setting Up Visual Studio Code for Debugging ASP.NET Core
I have been using VS Code since it was first released after Build in 2015, but I have only been using it as an editor never taking advantage of the Debugging capabilities it has available. In this post, I am going to walk through everything that is needed to get a new ASP.NET Core with an Angular front end to run via VS Code’s debugger.
On Windows, after the application has been generated and you are in the application directory you can use the following command to open the directory in VS Code.
I am sure there is something similar on Linux and Mac, but I don’t have the environments to try on.
VS Code Overview
When VS Code opens you will see a view close to the following.
The icons down the left side of the screen are for Explorer (shows currently open directory and files), Seach, Source Control (git support is built in), Debug, and Extensions.
The Debug tab will be our focus so click on it which will take you to the following view.
Using the gear with red circle select .NET Core as the environment for the project.
If you don’t see .NET Core listed click More… and click install for the C# option.
After selecting an environment VS Code will add a launch.json file to the project. This file defines what happens when the start button is clicked in the debugger. At this point clicking the start button to run the application using the debugger will result in an error that Could not find the preLaunchTask ‘build’.
Next, click the Configure Task Runner option and select .NET Core.
This will add a task.json file with a build command that the launch.json is looking for. At this point, I had to restart VS Code to get it to properly pick up the new files. This seems to be an issue that will be fixed with the next release of VS Code and can be tracked using this issue.
After restart and trying to run the debugger again I ran into the error Run ‘Debug: Download .NET Core Debugger’ in the Command Palette or open a .NET project directory to download the .NET Core Debugger.
I ended up having to uninstall and reinstall the C# extension and then opening a C# file to get the debugger to download. If you are having this problem make sure and open a C# file before going as far as reinstalling the C# extension.
Hitting run in the debugger now give the error launch: launch.json must be configured. Change ‘program’ to the path to the executable file that you would like to debug.
To fix this issue click Open launch.json and you will find two places with the following.
Change both places to point to the dll your application builds. In the case of my project named DebugTest the final version ended up being the following.
Debugging now works! Based on this post it would seem like debugging in VS Code is a big pain, but really after you get it set up once it just works. For new projects, you just have to let it add the
For new projects, you just have to let it add the launch.json and tasks.json and then set the path to your project’s assembly in launch.json. After that, you are ready to go.
I wait too long to figure this process out. I hope this helps you get started with debugging in VS Code.