How to Setup Visual Studio Code to Debug .NET Appications

The ability to find and fix bugs before releasing the software into production makes debugging one of the most crucial aspects of software development. Instead of using Console.WriteLine() statements at every intersection of your code, use VS Code’s many built-in functionalities, which include debugging tools to speed up and simplify the debugging process.

In this article, you will learn how to install the Microsoft C# extension, make debug configuration files, and set breakpoints in order to set up VS Code to debug C# and.NET Core applications.

Requirements

Before start, you need to install:

Microsoft’s C# extension offers strong yet portable development tools to enable cross-platform C# development in VS Code. Additionally, this extension offers wonderful features like:

1. Create a Simple C# Console Application

Let’s use the.NET CLI to create a straightforward C# console project using the dotnet new <TEMPLATE>command, where <TEMPLATE> is the application type, in order to avoid making the debugging process overly complicated.

Use the command dotnet new console --name <NAME> to create the C# console application. The <NAME> parameter specifies the name of the C# console project and the directory to be created. If the <NAME> parameter is omitted, the current directory name will be used by default.

dotnet new console --name SimpleConsoleProject && cd SimpleConsoleProject && code.

The aforementioned commands will launch the.NET project scaffolding tool, produce a basic Hello World starter console application, configure it, and launch the newly produced project in Visual Studio Code.

The C# extension will display a popup with the message Required assets to build and debug are missing from ‘SimpleConsoleProject’ as soon as Visual Studio Code has finished configuring the workspace environment. Add them in? – Click Yes to automatically generate and output the launch.json and tasks.json files into a .vscode folder.

Note: This is only possible if you have the C# extension by Microsoft installed.

When you open a C# file in VS Code, it will prompt you to install the C# extension if you don’t already have it.

2. Create C#’s Build and Debug Assets.

You can generate the build and debug files by clicking the Generate C# Assets for Build and Debug button on the debugger tab in VS Code’s left sidebar if you somehow missed the popup notification to do so.

The files required to build, debug, and launch the C# application will be generated when the button is clicked.

The tasks.json file contains the configurations for tasks (e.g. build, publish, watch, launch, etc) that are to be executed by VS Code.

The launch.json file contains the debugging configuration information.

Visual Studio Code can build and launch the project in debug mode thanks to these two files.

Now open the VS Code debugger panel and the launch.json file. To view the available tasks on the debugger panel, select the dropdown menu next to the play button.

.vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      // Use IntelliSense to find out which attributes exist for C# debugging
      // Use hover for the description of the existing attributes
      // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
      "name": ".NET Core Launch (console)",
      "type": "coreclr",
      "request": "launch",
      "preLaunchTask": "make this work",
      // If you have changed target frameworks, make sure to update the program path.
      "program": "${workspaceFolder}/bin/Debug/net6.0/SimpleConsoleProject.dll",
      "args": [],
      "cwd": "${workspaceFolder}",
      // For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
      "console": "internalConsole",
      "stopAtEntry": false
    },
    {
      "name": ".NET Core Attach",
      "type": "coreclr",
      "request": "attach"
    }
  ]
}
Let’s evaluate the first configuration (.NET Core Launch (console)) in the list. It launches the program ${workspaceFolder}/bin/Debug/net6.0/SimpleConsoleProject.dll , which is the .dll that will be generated when we build the application. The "args":[] property allows you to pass arguments to the main method as well.

In contrast to Visual Studio, which uses an external console, it also uses the internal console.

The VS Code debugger can be connected to an active instance of a.NET application using the second configuration (.NET Core Attach).

Now open the tasks.json and the launch.json files side-by-side to help us understand them better.

We have the "preLaunchTask": "build" in the launch.json file which corresponds to the "label": "build" task in the tasks.json .

The "label": "build" task runs the dotnet build command and specifies the project to build in the "args": [] property.

We also have two additional tasks "label": "publish" and "label": "watch" .

.vscode/tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "command": "dotnet",
      "type": "process",
      "args": [
        "build",
        "${workspaceFolder}/SimpleConsoleProject.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "problemMatcher": "$msCompile"
    },
    {
      "label": "publish",
      "command": "dotnet",
      "type": "process",
      "args": [
        "publish",
        "${workspaceFolder}/SimpleConsoleProject.csproj",
        "/property:GenerateFullPaths=true",
        "/consoleloggerparameters:NoSummary"
      ],
      "problemMatcher": "$msCompile"
    },
    {
      "label": "watch",
      "command": "dotnet",
      "type": "process",
      "args": [
        "watch",
        "run",
        "--project",
        "${workspaceFolder}/SimpleConsoleProject.csproj"
      ],
      "problemMatcher": "$msCompile"
    },
    {
      "label": "test",
      "command": "dotnet",
      "type": "process",
      "args": ["test", "${workspaceFolder}/SimpleConsoleProject.csproj"],
      "problemMatcher": "$msCompile"
    },
    {
      "label": "make this work",
      "dependsOn": ["build", "test"],
      "problemMatcher": "$msCompile"
    }
  ]
}

You are also allowed to add your own tasks. For example: create a task called "label": "test" and "label": "make this work" that will depend on the build and test tasks.

Next, add the "label": "make this work" task to the "preLaunchTask": "" property in the launch.json file.

3. Create a Breakpoint

A breakpoint is a place in the code where you want the code to stop running so that you can inspect the memory addresses and get a thorough understanding of the memory’s current state.

For instance, the code will run and freeze at line 5 if you set a breakpoint at that location and run the program in debugging mode.

Click on the gutter next to the line numbers in the editor to set a breakpoint. Red circles will appear on each line as you move your mouse over the gutter.

To set a breakpoint on that line, click the red circle on the gutter where you want to stop the execution of the code.

4. Start VS Code Debugger

Now, launch the debugger by pressing F5 or the play button located in the VS Code debugger panel. The program will run with the first breakpoint’s code paused, allowing you to check the status of the variables in the VARIABLES pane.

Additionally, you can view the values of the variable by hovering your mouse over the line of the current breakpoint, which is highlighted in yellow.

Next, move the code execution line by line using the debugger’s navigation buttons.

You can view the value of a variable or an expression from the current state of the program using the WATCH panel in Visual Studio Code. If you want to see some information as your program is being run from line to line, the WATCH panel can be helpful.

5. Install Recommended VS Code Extensions

Let’s talk about the various extensions that I suggest you use when writing C# and DotNet code. These are the extensions I will install without hesitation in order to give Visual Studio Code an IDE-like experience.

1. Roslynator

2. .NET Core Test Explorer

3. Auto-Using for C#

4. C# Namespace Autocompletion

Conclusion

You learned how to set up VS Code to debug C# and.NET programs in this thorough tutorial. With this information, you can begin easily debugging your projects in VS Code.

If you are looking for fast and secure ASP.NET hosting, you can visit our site at https://www.asphostportal.com. Our fully featured hosting already includes

  • Easy setup
  • 24/7/365 technical support
  • Top level speed and security
  • Super cache server performance to increase your website speed
  • Top 9 data centers across the world that you can choose.

 

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *