ASP.Net5 website running on Linux in Azure

MicrosoftLoveLinux-bigThis post will be on the subject Microsoft <3 Linux where I will deploy an ASP.Net5 website on an Ubuntu Linux server that runs on Azure. Since Microsoft open sourced ASP.Net5 and the CoreCLR, it can’t get any more Microsoft <3 Linux than this. It’s also a continuation from the last post, where I deployed an ASP.Net5 website onto a Nano Server running on Azure. This post will be a bit more command line level and will show you how the new DNX (.Net Execution Environment) framework works. All the DNX related commands are cross platform and work on Windows and Mac too.

The idea to the post was the ending of Scott Hanselman’s //build session (see refs), where he very quickly shows running an ASP.Net5 website on a Raspbery pi. How did he do that, I thought? Well, that’s what I’m going to show you.

Ubunto on Azure
Start by creating an Ubuntu 14.04LTS Linux server on Azure. If you provision it into one of your existing Virtual Networks, please make sure that your DNS is working properly or much of the scripted deployment will go bonkers otherwise. No special settings and you can pick an A1 size if you like. Once it is created, make sure everything is updated by running the command

Bash_script_01

That will take a while to run and will produce a few y/n prompts and also screens confirming you want to stay with the same GRUB boot manager (which you want). Just hit Enter and continue.

Install Mono and other tools
We are going to use the Mono CLR Framework, which is an open source implementation of the ECMA standard for CLR that works on Mac, Linux and Windows. You install the complete Mono stack with the following command

Bash_script_02

We also need to install libuv and git on the VM, so continue with the series of commands
Bash_script_03

DNX .Net Execution Environment
DNX?? The .NET Execution Environment (DNX) is a SDK and runtime environment that has everything you need to build and run .NET applications for Windows, Mac and Linux. DNX was built for running cross-platform ASP.NET Web applications, but it can run other types of .NET applications, too, such as cross-platform console apps.

We need to get the DNX installed so we can do the same things Visual Studio 2015 does but from the command line. To download it, use the following command

Bash_script_04

When I tried the next steps, It crashed in DNX related commands and I was stuck for a while when trying a lot of possible tips in different foums. Crashes where both CLR related and native crashes in underlying libraries. It took me a while to find a way how to work around it, but combining two details from two different blogs, I got the DNX to perform if I did the following before continuing. The solution was the export MONO_THREADS_PER_CPU and the service stop command that stops the Name Service cache.

Bash_script_06

It might be that this is only bugs when working with the beta7 runtime and that you don’t need it, but at the point of writing this, I could not get by it.

As you can see above, dnvm list first says that the .Net Execution Environment isn’t installed, we then install it using the DNVM UPGRADE command, which gives us the mono runtimewith version 1.0.0-beta7.

Downloading sample code and run it
You can download the ASP.Net5 sample code from github the following way, which is the easiest way just to have a sample webapp to test that everything is working. I use the HelloWeb sample since it is as simple as it can get.

Bash_script_07

In the HelloWeb directory you have only a few files after downloading it from github. To download all dependencies and dlls referenced, you have to run the DNU RESTORE command. Dnu restore will look into the project.json file and download all NuGet packages needed to build and run the webapp. This step crashed 100% of my attempts until I found the solution vid export and stop service.

Bash_script_08

If the dnu restore command worked without errors, you start the webapp by invoking the command dnx kestrep. DNX is the run command and you ask it to look for the kestrel way of running the app, where kestrel is defined in project.json. You can see the kestrel line under commands in the project.json file below.

Bash_script_09

When you start the webapp with the dnx kestrel command, it is a little bit of a non-event. It just says “Application starting” on the console as a confirmation.

Bash_script_10

Lastly, you need to create an Azure Enpoint for public port 80 mapping to private port 5004 for http requests to flow through to the server.

Voila – you have a website built in C# using DNX and Mono running in Azure on a Ubuntu Linux server. How many solutions will be developed in C# and deployed to Linux is perhaps something that the future will tell. But atleast you know it will be possible

Endpoint

Sidenote

It is today 12 years since David Stutz book about Rotor (Shared Source CLI) was published and 17 years since he started to work on the CLR at Microsoft. I had the priviledge to discuss the early works he did at a conference at that time. He would have loved what is happening today.

References

Bash script commands used in this post
http://data.redbaronofazure.com/public/dnvm_aspnet5_website.sh

ASP.Net5 official guide to install a webapp on Linux
http://docs.asp.net/en/latest/getting-started/installing-on-linux.html
In all honestly, I didn’t follow this procedure since the below is a better once

Jeremy Morgans blog about the same topic- He uses the K tools (Katana), which is the predecessor to DNX. Still, much of the commands he uses are the ones I use too.
https://www.jeremymorgan.com/blog/programming/how-to-asp-5-linux/

Another blog on the same topic which highlighted parts of the workaround for the dnu/dnx crashes
http://dave.ninja/2015/07/30/asp-net-on-linux-part-2-installing-the-net-framework/

ASP.Net5 Open Source repository
https://github.com/aspnet/home