3. Project Atropa: How to Get ASP.NET 5 Running on Linux

Loading

Raspberry Pi Hacks – Facebook – In the previous parts of the series, we discussed why and how we would create a “rogue” computer based on a Raspberry Pi and Raspbian Linux, and showed how to install it. In this part, we will show how to install Mono and ASP.NET 5 on it and how to write and run a simple application. Although the procedure is tailored to the Raspberry Pi and Raspbian, it should also be generally usable with other Linux distributions.

Animal repellent with a loudspeaker and control unit

ASP.NET 5 Architecture

Today, only one runtime (.NET Framework) is effectively supported, and ASP.NET depends on a number of old APIs tightly bound to Windows and IIS. Running ASP.NET on anything other than Windows is practically impossible. This is what ASP.NET looks like today, in version 4.5:

With ASP.NET 5, the architecture will change significantly. The new version will be independent of the operating system, and Windows, Mac OS X and Linux will be treated as equal platforms. We will also be able to choose among three different platforms called DNX (.NET Execution Environment), each with different characteristics. All web technologies will be built purely on these, without depending on specific legacy APIs. It can be pictured roughly as follows:

We currently have a total of three different runtime environments – DNX:

  • .NET Framework 4.6 – the full .NET we know and love. It runs only on Windows and offers the complete API set and the widest range of capabilities.
  • CoreCLR – a new runtime written from scratch. It currently offers the fewest capabilities and is not yet usable in practice, but it should gradually replace the .NET Framework and become available for all three supported platforms (for now, it is available only for Windows).
  • Mono – the Mono project gains a new purpose with the arrival of ASP.NET 5. It works across platforms – Windows, Linux and Mac – and is mature enough for a range of applications.

Installing the Mono Runtime

In an ideal world, typing sudo apt-get install mono-complete would suffice. However, we do not live in an ideal world, and the current version of Mono is not available in the Raspbian repositories at the time of writing. The version that is available does not work particularly well and is incompatible with ASP.NET 5.

The first two commands add the Mono distribution server to the package manager’s list of repositories. The remaining commands update the list of available packages, install the available applications and finally install the complete Mono distribution. They will take a while to run because many things need to be downloaded and installed.

Installing Libuv

ASP.NET 5 uses the Libuv library for asynchronous programming. Unfortunately, it is not available as a package. Start by visiting the project’s GitHub page and checking the current version. At the time of writing, the current version was 1.6.1. If a newer version has been released in the meantime, change the version number in the commands.

At the time of writing, the current version was 1.6.1. If a newer version has been released in the meantime, change the version number in the commands.

Use the following commands for installation:

The first command installs the automake and libtool packages, which we will need later. The second downloads the library’s source code and extracts it into a temporary folder in / usr / local / src. The remaining commands compile the code and delete the temporary files.

Advertisements

Adding Certificates to the Trusted Store

ASP.NET 5 continually downloads packages from various servers. It communicates with them over HTTPS.

Installing ASP.NET

Now we can finally install ASP.NET 5 itself. Everything is based on DNVM – .NET Version Manager. This allows different versions of .NET to be installed side by side.

However, no versions of DNX are installed, as you can verify with dnvm list. To install the latest beta, use dnvm upgrade. At the time of writing, this is version 1.0.0-beta5, as shown in the following output from that command:

If a newer version is available when you install ASP.NET, you must also modify the following files and projects accordingly.

Note: in previous versions, DNVM was called KVM and different commands were available. If you find examples with too many “k”s, they are probably outdated.

ASP.NET 5 installation is now complete, and we can create the first project.

Creating a Sample Project

Our “rogue captive portal” will be called WiFiGate, and the folder names and similar items reflect that. If you want to run your own application, name them differently.

We will begin by creating a www directory in the current user pi’s home directory, and a wifigate directory inside it. Use the following command: mkdir -p ~ / www / wifigate.

Then create a project.json definition file in that directory. We will generally use the nano editor to edit files, because it is reasonably comfortable and Windows users will probably find it quite approachable. Start it with nano ~ / www / wifigate / project.json.

Enter the following code in the file (remember to modify it if you use a version newer than 1.0.0-beta5; in Nano, you can replace text using Ctrl + W Ctrl + R):

Advertisements

Exit the editor by pressing Ctrl + X. When asked whether to save the modified contents, answer Y (yes). Keep the proposed filename and confirm it by pressing Enter.

In the same way, create a Startup.cs file in the same folder with the following contents:

This code essentially does nothing except display a sample page so that we know everything is working correctly.

Restoring NuGet Packages and Running the Project

Before starting the application, you need to install (restore) the packages specified in project.json. Do this with dnu restore ~ / www / wifigate /.

Now we can finally run the project in the built-in Kestrel server with the following command: DNX ~ / www / wifigate / Kestrel.

Startup will take a while, but after “Started” appears in the console, we can visit the RPi’s IP address on port 5004 (configured in project.json). If you have done everything correctly, the sample ASP.NET 5 page should appear:

Congratulations, you have just got an ASP.NET 5 application running on a Raspberry Pi under Linux.

Normally, you should be able to stop Kestrel by pressing Enter. However, the current beta contains a bug, so slightly more drastic measures are necessary.

First, suspend the process with Ctrl + Z. Then use ps to find the process named mono and identify its pid (in the first column; 18080 in this example). Kill the process using kill -9. You can verify that everything worked by running ps again. The whole sequence is shown here:

Our application is running, but only on the development server, and we have to start it manually; it does not run in the background as a service. We will show how to arrange that in the next part in a week’s time.

Link to the next – upcoming article.

  1. Project Artropa: How to Turn a Raspberry Pi into a Rogue Device with .NET
  2. Project Atropa: Setting Up Raspberry Pi and Raspbian Linux
  3. Project Atropa: How to Get ASP.NET 5 Running on Linux
  4. Project Atropa: Automatically Starting the Website and Publishing through nginx
  5. Project Atropa: Creating a Honeypot
  6. Project Atropa: Creating a Captive Portal
  7. Project Atropa: How to Defend Yourself?

Advertisements
AliExpress

Leave a Reply

Your email address will not be published. Required fields are marked *