Welcome to the first in a series of articles focused on .NET Core from a devops perspective where I hope to demonstrate how we can practically start developing and deploying .NET Core applications on Linux.
Part 4: Setting up a CI build and deploy pipeline for our ASP.NET Core Service using Docker
As a devops engineer working a lot on the Microsoft stack, it is second nature nowadays to automatically setup and configure farms of Windows web servers running IIS and deploy ASP.NET based web sites and services to them. But now with .NET Core and ASP.NET Core it is possible to run those same web sites on Linux.
In my opinion, one of the main motivations to use Linux is cost. In Azure, Linux VMs are approximately half the price of Windows VMs. A D2v2 Windows Server is about £122/month whilst a D2v2 Ubuntu server runs at £62/month. If you are working at scale in the cloud then that is going to equate to a huge cost saving.
In this series of articles I hope to demonstrate that moving to Linux is possible and practical with a real life example including all the automated configuration and deployment processes.
Throughout these articles I am going to use an example application that I wrote called Downcount. Downcount is my take on the famous Countdown numbers game, where contestants are given 6 integers and 1 target integer. The aim is to derive the target number by adding, subtracting, multiplying and dividing the original 6 integers. It is a nice problem and solving it is a good way to learn and try out new programming languages.
The .NET Core implementation comes with a simple console application to try it out and uses an iterative, brute force approach. e.g.
The set of solutions to the game, y from (x1, x2, x3…xn), is equivalent to the set of solutions to
set of solutions [(y – x1) from (x2, x3, ..xn)] ADD x1 UNION
set of solutions [(y + x1) from (x2, x3, ..xn)] MINUS x1 UNION
set of solutions [(y / x1) from (x2, x3, ..xn)] MULTIPLY x1 UNION
set of solutions [(y * x1) from (x2, x3, ..xn)] DIVIDE x1 UNION
etc etc recursively
Running .NET Core using Visual Studio
There is full support for .NET Core in Visual Studio so if you are running Visual Studio 2015 Update 3 the simplest way to run Downcount in .NET Core is to open up the solution Downcount/DotNetCore/Downcount.sln, build and then run the Downcount.Console application direct from Visual Studio.
Running .NET Core from the Command Line on Windows
With .NET Core you don’t have to use Visual Studio, you could use another development tool like Visual Studio Code or you could work direct from the command line. It is interesting the see the command line tools that underpin .NET Core so lets do that.
You will need as a minimum the .NET Core SDK installed. With that installed you will have, among other things, the dotnet command line tool available to you.
Grab the latest code from the Git repo and cd into the Downcount.Console directory
git clone https://github.com/shiningdragon/Downcount.git cd Downcount\DotNetCore\Downcount.Console
Restore and build the application
dotnet restore dotnet build
Execute the console application
cd bin\Debug\netcoreapp1.0 dotnet Downcount.Console.dll
Alternatively you can run the application directly by running
from the Downcount.Console directory.
Please check out this reference article for more info about .NET Core command line tools and what exactly each of the above is doing.
Running .NET Core on Linux
Now for the main part. Running our lovely C# code on a Linux server. I am using an Ubuntu 16.04 VM that I created in Azure but feel free to try another distro if you wish.
First thing is first, SSH onto the server and install .NET Core. Check these instructions for other distros. For my Ubuntu machine it was
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 417A0893
sudo apt-get update sudo apt-get install dotnet-dev-1.0.0-preview2-003131
Install git if you haven’t already
sudo apt-get install git-all
Get the latest code
mkdir Downcount cd Downcount git clone https://github.com/shiningdragon/Downcount.git cd Downcount/DotNetCore/Downcount.Console
Build and run the application
dotnet restore dotnet run
Then you can see Downcount and all that C# code running on Linux! Jump to Part 2 of this series to see how to host your first ASP.NET application on Linux.