.NET Backend Tools

Debugging ASP.NET Core apps in Visual Studio Code

Debugging ASP.NET Core apps in Visual Studio Code is not as simple as in „Windows version” but it’s still pretty easy to setup. So if you have ever had some troubles with that, here’s an explanation of the entire process.

 

Adding configuration

Before configuring VSC, you need to install C# extension if you don’t have it yet. I’d say, it’s a must-have for every .NET developer working with Code since it offers lots of features like IntelliSense, useful snippets, quick code fixes/refactor actions any much more. When you have the extension, go to the debug tab and click on the „Add Configuration…„:

 

 

You’ll be asked about target environment. Select .NET Core and press Enter. You should see a file called launch.json which contains three different configurations:

 

{
	"version": "0.2.0",
	"configurations": [
	
		{
			"name": ".NET Core Launch (console)",
			"type": "coreclr",
			"request": "launch",
			"preLaunchTask": "build",
			"program": "${workspaceRoot}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",
			"args": [],
			"cwd": "${workspaceRoot}",
			"stopAtEntry": false,
			"console": "internalConsole"
		},
		{
			"name": ".NET Core Launch (web)",
			"type": "coreclr",
			"request": "launch",
			"preLaunchTask": "build",
			"program": "${workspaceRoot}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll",
			"args": [],
			"cwd": "${workspaceRoot}",
			"stopAtEntry": false,
			"launchBrowser": {
				"enabled": true,
				"args": "${auto-detect-url}",
				"windows": {
					"command": "cmd.exe",
					"args": "/C start ${auto-detect-url}"
				},
				"osx": {
					"command": "open"
				},
				"linux": {
					"command": "xdg-open"
				}
			},
			"env": {
				"ASPNETCORE_ENVIRONMENT": "Development"
			},
			"sourceFileMap": {
				"/Views": "${workspaceRoot}/Views"
			}
		},
		{
			"name": ".NET Core Attach",
			"type": "coreclr",
			"request": "attach",
			"processId": "${command:pickProcess}"
		}
	]
}

 

  • .NET Core Launch (console) – runs your app in a terminal
  • .NET Core Launch (Web) – runs your app in a terminal and opens it in a web browser
  • .NET Core Attach – you specify the process of running app and attach to it

First two configs require some extra work from you. If you take a closer look, you should spot this line in each one:

 

"program": "${workspaceRoot}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll"</div>

 

As it’s described, you need to specify the path to the *.dll file of your app (so you need to build it, before you go further). In my case this line looks as follows:

 

"program": "${workspaceRoot}/bin/Debug/netcoreapp1.1/Debug.VSCode.dll"</div>

 

When you’re done, save the file and run debug in console or web mode. You should see the following error:

Could not find the preLaunchTask ‚build’

That is because both depend on the task called build which was not specified. Click on the „Configure Task Runner” and select .NET Core. Another file called tasks.json will be generated for you. It contains mentioned build task:

 

{
	// See https://go.microsoft.com/fwlink/?LinkId=733558
	// for the documentation about the tasks.json format
	"version": "2.0.0",
	"tasks": [
		{
			"taskName": "build",
			"command": "dotnet",
			"type": "shell",
			"group": "build",
			"presentation": {
				"reveal": "silent"
			},
			"problemMatcher": "$msCompile"
		}
	]
}

 

One thing we need to fix here is to change the value of the command property from „dotnet” to „dotnet build„.  Save the file and you’re ready to go.

 

Debugging ASP.NET Core app

Let’s see whether it’s working starting from Launch console mode. Choose it from the select box and start debugging. After few seconds you should spot additional windows for debug process on top of the VSC window:

 

 

I do my tests on default ASP.NET Core Web API so I create new breakpoint inside ValuesController and make a call from a browser. The result?

 

 

As you see, a breakpoint has been hit. I have also an access to variables (this) and all messages in the console. Launch (Web) mode is very similar to this, therefore I’ll skip it.

If you want to attach to running app, you need to run it first on your own using dotnet run command. Then go back to VSC and select a proper config from the select box and choose a process:

 

 

After few second you should be attached and ready to debug.

Don’t miss new posts!

If you enjoy reading my blog, follow me on Twitter or leave a like on Facebook. It costs nothing and will let you be up to date with new posts 🙂