3 posts

Debugging C# Code with Visual Studio Code and Omnisharp

Visual Studio Code on its own is just a lightweight code editor however Omnisharp's C# extension includes debugging capabilities that makes VsCode a productive C# development environment.


To write and debug C# applications on Linux the following prerequisites must be installed:

Enabling Debugging

When opening a new C# project for the first time in VsCode a popup window should display asking if you would like to add debug assets to your project - Select Yes when you see this message.

You should be able to see a new .vscode folder in your project which contains 2 files - tasks.json and launch.json.
The Tasks.json file should be populated with configuration data for the dotnet build task so that VsCode can build out project when we click the launch button.

    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
            "taskName": "build",
            "args": [
            "isBuildCommand": true,
            "problemMatcher": "$msCompile"

The Launch.json file contains configuration data for the available debugging modes which by default are launch (from VsCode) and attach (to a running process).

    "version": "0.2.0",
    "configurations": [
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceRoot}/bin/Debug/netcoreapp1.1/newconsole.dll",
            "args": [],
            "cwd": "${workspaceRoot}",
            "console": "internalConsole",
            "stopAtEntry": false,
            "internalConsoleOptions": "openOnSessionStart"
            "name": ".NET Core Attach",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:pickProcess}"

Launching the Debugger from VsCode

Launching the debugger from VsCode is a very simple process for Console and Web projects, simply set a breakpoint in your code, navigate to the debug window (ctrl+shift+d) and hit the play button - the ".Net Core Launch" option should be selected by default. Your application should now stop at your breakpoints.

Attach to a process/website

Attaching the debugger to a running process is also very easy with VsCode, set your breakpoints, select the ".Net Core Attach" option from the debug menu and hit play. The task bar should show a list of running processes you can choose to attach your debugger to - in this example we shall attach to a running dotnet website process.

Exploring the Debug panes

The debug window in VsCode may seem heavily stripped down compared to the fully fledged version of Viual Studio however it provides enough functionality to cover the majority of use cases with the following panes:

  • Local Variables
  • Watch Window
  • Call Stack
  • Breakpoints

The local variable and watch windows provide essential variable tracking and modification functions while the watch window also enables the execution of methods on the fly including LINQ extension methods which is extremely useful.

The call stack window functions the same as its full fledged Visual Studio counterpart enabling crawling back up through the code execution heirarchy with full variable tracking.

Debuging on non-standard linux distros

If you've been following the older posts on this blog you will have noticed that I'm using VsCode on my Chromebook which is running a non-standard Liux Distribution based on Ubuntu 16.04 - GalliumOS. As of writing this article, Omnisharp executes its debugger assembly based on the identifier of the operating system running VsCode (in my case galliumos-2.1: an unrecognized operating system).

Luckily for us VsCode and all its extensions are written in Javascript meaning that we can get around this by editing a *.js file.

Find the location of your omnisharp extension on your machine and open the platform.js file. On my machine the path is:


In here there is a function called getRuntimeIdHelper which dictates what distribution to use. To get it to work with GalliumOS v2.1 I just had to add a new case block to the switch statement to handle GalliumOS and now C# debugging works with GalliumOS.

case 'galliumos':  
    return ubuntu_16_04;

I hope this method works for others building .net core apps using other non-standard Lunix distributions.

C# Development on Linux with .Net Core & Visual Studio Code

C# Development on Linux with .Net Core & Visual Studio Code

Framework and Prerequisites

Before we can start writing C# code on our machine, there are a few packages we need to install as prerequisites.

With these software packages installed you should be good to go. Your system is now ready to start writing, building and running C# code with any code editor. Although this guide is focused around using Visual Studio Code, please skip to the next section if you'd prefer to use a different code editor.

Setting up Visual Studio Code

Download the latest version of VsCode here and follow the installation instructions for your Linux distribution. If you are running a Debian/Ubuntu based Linux distro, download the *.deb installer and install VsCode with the dpkg command.

sudo dpkg -i <VsCodeFileName>.deb  

Once you've installed VsCode, open it up and install the C# extension by Omnisharp which will provide intellisense, autocomplete and other helpful notifications in your editor.

Creating and Running C# Projects

Creating a new .Net Core project is easy and can be done from the .Net Core command line interface using the "dotnet new" command followed by the type of project you want to create. For this example we will be creating a simple console app but you can find the documentation about the .Net Core CLI https://docs.microsoft.com/en-us/dotnet/articles/core/tools/.

To create a new project, first of all create a directory for the code to sit and then use the dotnet new command.

mkdir myconsoleapp  
cd myconsoleapp  
dotnet new console  

In order to build/run the project we need to download the project dependencies using the "restore" command, build it with the "build" command and run it with the "run" command.

dotnet restore  
dotnet build  
dotnet run  

When running the default C# console project, the program should display a simple "Hello World!" message.

We can dive in to VsCode and look at the code of our current working directory using the "code" command followed by a full stop (period).

code .  

The dotnet cli should've created a myconsoleapp.csproj project file and a Program.cs code file containing the following code.

using System;

namespace myconsoleapp  
    class Program
        static void Main(string[] args)
            Console.WriteLine($"Hello World!");

In VsCode you should be able to edit the code, save the file then build and run it via the command line. You may even build and run the code inside VsCode using the integrated terminal (View > Integrated Terminal)

Installing Nuget Packages

An important aspect of .Net Development is being able to make use of third party tools and packages available via Nuget.

For those who have used earlier versions of the .Net Core framework this used to be easy as new projects used to contain a project.json file which has a list of all the Nuget Packages/Dependencies in a project which could be edited easily with full intellisense in VsCode.

Sadly this isn't the case now, following the version 1 release of .Net Core, the team decided to revert back to the old XML based *.csproj based files which are an absolute pig to edit manually in an editor.

Thankfully for us there is now a Nuget Package Manager extension available for VsCode that enables us to do so. Install this extension then you'll be able to install Nuget Packages into your .Net Core projects via the command pallet in VsCode and your project file will be updated automatically.

For this example let's add the popular DateTime package NodaTime to our project using the following commands from the VsCode command Pallette.

Nuget Package Manager: Add Package  

Now we should be able to reference the NodaTime package with a using statement in our code and call the library with full intellisense (thanks Omnisharp!). I've added some NodaTime code to our previous example here.

using System;  
using NodaTime;

namespace myconsoleapp  
    class Program
        static void Main(string[] args)
            Instant now = SystemClock.Instance.GetCurrentInstant();
            Console.WriteLine($"Hello World!, the time is {now.ToString()}");

Now if we restore, build and run our app we should get our hello message with the current date from NodaTime.

Enjoy writing C# code with your editor of choice on Linux!

Follow up topics will include debugging and unit testing .Net Core applications on Linux.

Web Development on a Chromebook Part 2: Setting Up Development Tools

Web Development on a Chromebook Part 2: Setting Up Development Tools

Following on from part 1: Installing GalliumOS on a Chromebook - we should have full access to install applications and developer tools onto the Chromebook through GalliumOS (or any other Linux distribution). There are many tools and editors available for web development, in this tutorial we're going to setup a complete web development environment by installing NodeJS - for running server side Javascript and installing packages, Git - for source control, Visual Studio Code my preferred code editor and a couple of NPM packages to generate some skeleton web applications to get us up and running quickly.

Installing NodeJS

NodeJs can be installed via the command line using the standard sudo apt-get install command however with GalliumOS, I'd strongly advise installing node via the Node Version Manager (NVM). I found this easier because when Node is installed using sudo, all node commands need to be executed with sudo permissions however this falls over when automated scripts try to run node/npm commands without sudo permissions and it becomes difficult to maintain. Using the NVM instead enables you to run any node/npm command without using sudo and it also enables multiple versions of node to be installed and managed on the same machine with he ability to switch versions on demand which is pretty cool.

To install the Node Version Manager run the following command:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash  

Once the NVM has been installed we can install the latest version of NodeJS

nvm install 6.10  

One installed, run the following command to double check that you have a version of NodeJs installed:

nvm ls  

Now node is installed, we just need to install Node's package manager (NPM) and we're ready to install VsCode

npm install -g  

Installing Visual Studio Code

To install Visual Studio Code we first need to get the installer from the VsCode Website (download the Debian *.deb installer and install it from the command line.

sudo dpkg -i <VsCodeFileName>.deb  

Now we're all setup, navigate to a directory on your file system and run the following command to launch VsCode

code .  

We've now turned our ChromeBook into a portable web development machine, stay tuned for more posts on how to be productive using visual studio code.