All Articles

Quick notes on running C# on WSL and debugging it in VSCode

This page has been machine-translated from the original page.

This article is a memo for quickly preparing an environment where you can run C# programs on WSL.

We will build an ELF-format console application with .NET. We will also configure debugging for a C# program through VSCode connected to WSL.

Install .NET 5.0 on WSL Ubuntu

First, to create programs in C#, install the cross-platform .NET 5.0 on Ubuntu running under WSL.

Download .NET (Linux, macOS, and Windows)

For installation, the same commands as the Debian 10 instructions in the following document are sufficient.

Install .NET on Debian - .NET | Microsoft Docs

wget https://packages.microsoft.com/config/debian/10/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb

# Install the SDK
sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-5.0

Install the extension in VSCode

Next, install the extension needed to debug C# in VSCode.

If you search for C#, just install the extension that appears at the top. Make sure VSCode is connected to WSL when you install it.

image-20210602195127867

Create a .NET project

Next, create a .NET project on WSL. Create it as a console application.

dotnet new console -o <project_name>

Once the project has been created, there should be a directory in the current directory with the same name as the one you specified for project_name.

Verify that it can be run from the command line

If you move into the project directory you created and run the run command, the code in Program.cs will be executed. When creating and debugging a C# program, this is the file you will edit first.

cd <project_name>
dotnet run

Debug in VSCode

Since we have confirmed that it runs from the console, next let’s try running it under the debugger from VSCode. The following two files are used at this point.

  • launch.json
  • tasks.json

Create both of them directly under the .vscode directory.

When you debug a .NET application on WSL from VSCode, the files above that are usually generated automatically do not work properly. Because of that, you need to manually add the information required for C# debugging to these files.

The contents that need to be written in each are as follows. In both cases, note that project_name must be replaced with the name of the project you created.

  • launch.json
{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/procon_cs/bin/Debug/net5.0/<project_name>.dll",
            "args": [],
            "cwd": "${workspaceFolder}/<project_name>",
            "console": "internalConsole",
            "stopAtEntry": false
        },
        {
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach"
        }
    ]
}
  • tasks.json
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "command": "dotnet",
            "type": "process",
            "args": [
                "build",
                "${workspaceFolder}/<project_name>/<project_name>.csproj",
                "/property:GenerateFullPaths=true",
                "/consoleloggerparameters:NoSummary"
            ],
            "problemMatcher": "$msCompile"
        }
    ]
}

With that, the setup is complete, so try debugging your C# program from VSCode.

Open the debug toolbar from the left sidebar and run the .NET program under the debugger.

Summary

I wanted to be able to casually write C# on WSL, so I set up this environment on a whim.

I love C#, but launching Visual Studio every time is a bit of a hassle, so I did not have many chances to write it before. Now that I can run it easily from VSCode, I hope to study C# more going forward.