.NET Core on Linux

Introduction

Just a few years ago, Microsoft was decidedly anti-Linux.

Mono – the cross-platform, open-source version of the .NET Framework – had been around since 2004. But it was independent of Microsoft, was always playing catchup, and not widely accepted.

So in 2014, Microsoft surprised everyone by announcing a new, cross-platform .NET!

.NET Core runs on Windows, Linux, and macOS. It’s a fairly large subset of the full .NET Framework, provides excellent performance, and is very much geared towards web applications using ASP.NET Core.

What We’ll Be Doing

On a Linux machine (Ubuntu 16.04) we’ll be installing .NET Core, and creating and running an ASP.NET Core web application.

Then we’ll install Visual Studio Code, a freely available source code editor (*) from Microsoft, and use it to build and debug a .NET Core web application.

(*) Visual Studio – which has been in existence since the 1990s – is Windows only, and paid software. Visual Studio Code, on the other hand, has only been around since 2015, and is cross-platform.

Right, let’s go…

.NET Core

Installation

Open up a terminal, and enter these four commands:


sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ xenial main" > /etc/apt/sources.list.d/dotnetdev.list'

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys B02C46DF417A0893

sudo apt-get update

sudo apt-get install dotnet-dev-1.0.4

Refer to https://www.microsoft.com/net/core#linuxubuntu for full details.

Creating a .NET Core Project

In the terminal, enter:


cd

mkdir dev

cd dev

dotnet new mvc --name webapp1

The dotnet command will take a minute or so while it creates a local cache “to improve restore speed and enable offline access”.

Now enter:


cd webapp1

dotnet restore

dotnet build

dotnet run

This will output text similar to the following:

Hosting environment: Production
Content root path: /home/grahamb/dev/webapp1
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

Open up a browser, and navigate to http://localhost:5000:

dotnetcore success

Success!

Return to the terminal, and press Ctrl+C to terminate the process.

Visual Studio Code

Installation

Go to https://code.visualstudio.com/Download and click the .deb box below Tux (the Linux penguin) to download the package.

In the terminal, enter:


sudo dpkg --install ~/Downloads/code_1.14.2-1500506907_amd64.deb

and wait for the installation to complete (note that the version number in the filename might be different for your download!).

Create a .NET Core Project

Return to the terminal, and enter:


cd ~/dev

dotnet new mvc --name webapp2

cd webapp2

code .

This opens Visual Studio Code in the current directory (~/dev/webapp2):

vscode 1 first launch

A new web browser window will also be opened, showing a Getting Started page for Visual Studio Code. Feel free to browse this documentation.

Double-click Program.cs or Startup.cs. You’ll see an information bar at the top; click Show Recommendations:

vscode 2 missing C# extension

Locate the C# extension in the list, click Install, wait a few seconds, click Reload, click Reload Window.

The Output pane will automatically open, and the C# dependencies will be updated.

Click Yes and Restore in response to the two messages:

vscode 3 restarting after installing C# extension

Wait for the NuGet (*) packages to be restored:

(*) What’s NuGet, some of you might ask? It’s a package manager originally created for Visual Studio, similar to npm (Node.js) and pip (Python).

vscode 4 after restoring packages

Now click the Debug icon on the left:

vscode 5 click debug icon

Click the dropdown beside the green debug icon at the top, and select “.NET Core Launch (web)”:

vscode 6 select configuration

Now click the green debug icon (or press F5). Debugging will start and a browser window will automatically open:

vscode 6 success

Success!

Return to Visual Studio Code and click the red stop icon in the toolbar.

As an exercise, I’ll let you experiment with debugging and stepping through the source code. With the cursor on a line – try the actions in HomeController.cs – press F9 to set a breakpoint. When you navigate to the page in the web browser, program execution will halt at the breakpoint. Press F10 to execute the current statement, or F5 to continue execution. If you’re a Visual Studio user, you’ll already be familiar with these keyboard shortcuts!

First Steps with Visual Studio

Introduction

If you’re an experienced software developer, there’s a good chance you’ve already used Visual Studio. If so, then you can skip this post; you won’t learn anything new here.

But you might be an experienced developer who’s never used Visual Studio. You might be wanting to move into web development with C# and ASP.NET  after years of writing Java, C++, or VB6 code.

You might be an experienced PHP developer who’s never even seen Visual Studio.

Or you might be a student or a hobbyist, looking for your first software development position.

Whatever your reasons for being here are, we’ll start at the beginning.

An Overview of Visual Studio

Visual Studio is an IDE (Integrated Development Environment) from Microsoft. It’s been in existence since the 1990s, and is very widely used by professional – and hobbyist – software developers around the world. It’s mostly focused on C#, Visual Basic .NET, F#, and C++ development, but also includes support for other languages and technologies including Python, JavaScript, Xamarin, and Android.

And don’t confuse it with Visual Studio Code which – whilst getting better all the time – has only been around since 2015. This is essentially a free cross-platform text editor, also from Microsoft, but with support for projects, building, debugging, and is highly extensible; a scaled down Visual Studio, if you like.

What We’ll Be Doing

We’ll be installing Visual Studio Community 2017, and creating a .NET Core web application.

The Community edition is free and intended for students and individual developers rather than the enterprise. It’s fully functional, but lacks more advanced features.

We’ll be creating a .NET Core web application because, frankly, .NET Core is all the rage right now. If you don’t know anything about it, it’s the new cross-platform edition of .NET that currently runs on Windows, Linux, and Mac, has been written from the ground up, and provides a lean subset of the full .NET Framework. If you don’t know anything about the original .NET Framework itself, check out Microsoft’s overview.

Installing Visual Studio

Downloading

First, you’ll need to download Visual Studio Community. Go to https://www.visualstudio.com/vs/community, then click “Download VS Community 2017”.

Installing

Run the downloaded executable file. After accepting the UAC prompt and clicking Continue on the first Visual Studio window, you’ll soon be presented with this page:

vs install 1 - workloads 1

vs install 2 - workloads 2

Check (tick) these four workloads:

  • .NET desktop development;
  • ASP.NET and web development;
  • Asure development;
  • .NET Core cross-platform development (scroll down for this one).

There’s no need to check anything else, but feel free to do so should you wish to look at other areas (such as game or mobile development).

Click Install. The Workloads page disappears, showing this:

vs install 3 - progress

You’ll need to wait a little while as the installation proceeds. How long depends on the workloads and components you selected, and how performant your machine is.

Eventually – assuming there were no hiccups – you’ll see this:

vs install 4 - complete

Click “Launch”. Then – on the following window – click “Not now, maybe later” (or – if you have a Microsoft account – you can sign in instead).

vs install 5 - after clicking launch

This is what you’ll normally see whenever you run Visual Studio:

vs install 7 - after clicking start visual studio

To prove this, close Visual Studio, then find it in the Start Menu. Why would I lie?

Creating a New Project

Now it’s time to create a project. Click “Create new project…” underneath “New project” (near the middle of the window).

vs install 8 - create new project

In the list of Installed Templates on the left, find and select .NET Core. Now select ASP.NET Core Web Application (.NET Core). I never use the default location (it’s too long and easy to forget!) so change it to somewhere more convenient – C:\Dev or C:\Projects are good suggestions. You can leave the name of the project as is.

Now click OK, and you’ll see…

vs install 9 - after clicking ok

Nothing should need changing; just ensure that Web Application is highlighted and that Authentication is set to No Authentication.

Click OK. The project has been created:

vs install 10 - project created

Press Ctrl+F5 to build and run the web application. After a short delay a web browser (*) should open, running the web application that Visual Studio just created for you:

vs install 11 - after running

(*) No flaming me for using Edge! I simply installed Visual Studio on a clean virtual machine.

That was easy, wasn’t it? Of course, it’s a default template so the website doesn’t really do much. But it’s a decent starting point, and you’ll probably use this a lot while you’re finding your feet.

Hopefully you won’t have any problems, but sometimes they happen. In which case, the Microsoft Community and Stack Overflow are good places to find help.

Reinvention

Reinvention

What does reinvention mean? Wiktionary (currently) says of “reinvent”:

1. To invent again something that has already been invented.
2. To adapt into a different form; to give a new style or image to.

I’m not attempting to reinvent something that’s already been invented. (Although relatively few software developers can claim to have done something that’s never been done before.)

So that leaves adapting into a different form: I’m reinventing myself as a web developer.

But first, a little background…

The Past

I started out as a professional software engineer back in 1998. Nearly two decades later, that pretty much puts me in the ‘experienced’ category of software developers.

Most of those nearly two decades has involved language X. I’m purposely not revealing what language X is, because it’s not important (*). What is important, is what my future holds.

(*) Of course, the real language X is important. It’s been around for a while, and certainly isn’t going anywhere. My goal here isn’t to offend anyone.

Now, after two decades of language X, you begin to realise that even though you write software for different domains, you’re effectively doing the same things every day. Things become too easy and unchallenging. You’re not learning anything any more. You begin to crave change.

The Future

So I see my future in web development. Not mobile application development. Not desktop application development. Not games. Not hardware.

Web development. The development of websites and browser-based applications.

At a base level, that’s technologies including C#, ASP.NET Core, JavaScript, HTML, CSS. Why these? I’m mainly a Windows developer, but have also developed cross-platform software that runs on Linux too (roughly a 90-10 split). I’ve a fair amount of experience with C# (not for websites), but until recently relatively little experience with the others.

Why?

The Internet continues to grow and evolve. I don’t have any figures – call me lazy! – but that’s an undeniable fact. Every day, more and more communities come online, more and more people gain access to the internet. It’s everywhere, and like it or not, it can pervade our every waking hour.

I’ve dabbled in the development of websites in the past. I’ve created a few simple websites, either using static pages or basic dynamic pages using CGI to process PayPal payments.

But they’ve been side projects. Not part of my main paying job, which has been desktop and server application development using – mostly – language X.

And after nearly two decades I’m ready to move on from language X, and learn new languages, technologies, and methodologies. I’ve already begun this journey, I’ve taken more than a few steps on the long road ahead. I’ve learnt a huge amount already, and I’ve realised that it’s well worth sharing this information. After all, after nearly two decades of software development I’m well-positioned to separate the “wheat from the chaff”, because the internet’s full of misinformation and, frankly, bad information.

So I’m going to share my learning and my  experiences with the internet as I transition into web development.

I’m reinventing myself. And I hope my journey inspires others out there.