Asp.net 5 On Linux
As you know, Asp.net is now cross-platform and open source. You can run Asp.net application on Linux, Windows, and Mac OS X.
Nadella (Microsoft CEO) said “Microsoft loves Linux”, and I say “I love both, Linux and Microsoft”.
In this post, I will talk about:
- Motivation for Asp.net 5 on Linux ;
- Installing Asp.net 5 on Linux (Ubuntu 15.04);
- Installing Visual Studio Code (VS Code);
- Install Node.js and related tools
- Install Yeoman, the tool for scaffolding Asp.net 5 applications
- Develop Simple Console Application on Linux
- Develop Asp.net MVC Web Application
2 Motivation for Asp.net 5 on Linux
“ASP.NET 5 is a new open-source and cross-platform framework for building modern cloud-based Web applications using .NET” (from http://docs.asp.net/en/latest/conceptual-overview/aspnet.html )
It is a good direction for Asp.net team to give us a tool capable of running on different platforms. We develop once and we deploy everywhere like in Java!
3 Installing Asp.net 5 on Linux (Ubuntu 15.04)
Steps for installing Asp.net 5 on Linux (Ubuntu 15.04) are:
- Install Mono;
- Install libuv;
- Install .NET Version Manager (dnvm)
After these three steps, we should run Asp.net 5 on Linux machine
3.1 Mono Installation
Mono is ”a software platform designed to allow developers to easily create cross platform applications” (from http://www.mono-project.com/ ). It is developed using C# and sponsored by http://xamarin.com
Commands to get Mono installed on Linux
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
This command imports the public key used by the package management system
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
the above command creates a list file for our mono
After this command, we have to reload our package database
sudo apt-get update
Once the package database is updated, it is time to install mono by this command
sudo apt-get install mono-complete
3.2 libuv installation
libuv is “a multi-platform support library with a focus on asynchronous I/O. It was primarily developed for use by Node.js and others” (from https://github.com/libuv/libuv). In our case it will be used by KestrelHttpServer.
Firstly, we need to install these tools, if they are not already installed on Linux: automake, libtool and curl.
sudo apt-get install automake libtool curl
After this command, we fetch libuv source by executing the following command and we extract it
curl -sSL https://github.com/libuv/libuv/archive/v1.4.2.tar.gz | sudo tar zxfv - -C /usr/local/src
Following commands are used to compile and to install libuv on the linux machine
cd /usr/local/src/libuv-1.4.2 sudo sh autogen.sh sudo ./configure sudo make sudo make install sudo rm -rf /usr/local/src/libuv-1.4.2 && cd ~/ sudo ldconfig
3.3 .NET Version Manager (DNVM) Installation
This command installs dnvm
curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh
At this stage, we have a machine capable of running Asp.net 5. We can check which .Net version by this command
4 Installing Visual Studio Code (VS Code)
Visual Studio Code (VS Code) is a brand new code editor and cross-platform (Windows, Linux, Mac OS X) from Microsoft.
VS Code installation on Linux is straightforward, just you download the zip file from this web page. https://code.visualstudio.com/Docs/setup .
Once downloaded; you make a new folder and extract VSCode-linux-x64.zip inside that folder. It is useful to create a link to the executable Code by this command:
sudo ln -s /path/to/vscode/Code /usr/local/bin/code
This way, we can run the command code . from any folder.
5 Installing node.js and Yeoman
From my point of view, I have found that one of the big changes in Asp.net 5 is the introduction of node.js and related tools (bower, grunt, gulp, yeoman, …). Any web application developer should learn node.js and related tools if he/she wants to stay competitive.
Yeoman as we can read on their web site is a “web’s scaffolding tool for modern webapps”. And Asp.net 5 on linux leverages yeoman for scaffolding Asp.net Applications.
To install node.js on linux, just you run this command:
sudo apt-get install nodejs-legacy npm
sudo npm install –g grunt-cli sudo npm install –g bower
Now it is time to install yeoman:
sudo npm install –g yo
Because we are working with asp.net; we need to install generator-asp.net as well for yeoman:
sudo npm –g generator-aspnet
At this stage, we can now start generating asp.net applications by yeoman
The following picture illustrates its output:
As we can see on this picture, different asp.net applications can be generated:
- Empty Application
- Console Application
- Web Application
- Web Application Simple (Without Asp.net Membership and Authorization)
- Web API Application
- Nancy ASP.Net Application
- Class Library
The following picture illustrates what we get once we choose to generate Console Application
As we can see on this picture, we have to give a name to our ASP.Net Application, and the tool tells us what to do for building and running the application.
Three important commands to be aware of are:
- dnu restore;
- dnu . run for console application ;
- dnx . kestrel for web projects
The following picture illustrates the output for dnx . kestrel
Once the server has started, we can go to http://localhost:5000 (this port can change in the config file …). The following picture illustrates our Web Application in Firefox browser
6 Debugging Issues
At the moment, it is easy to debug Node.js Applications, but for Console Applications, you can do it but it is not straightforward. Use this hack from this web site for console application https://isimplecoder.github.io/2015/05/04/debugc%23/ . It is written for Mac but it works on Linux as well.
For Asp.net Web Application, it is not feasible yet (http://stackoverflow.com/questions/30027201/how-can-we-debug-web-application-on-visual-studio-code)!
The following picture illustrates debugging Node.js Application
Following picture illustrates Debugging Console Application
In this post, I have tried to highlight my enthusiasm regarding the Asp.net 5 which is now cross-platform. It is not yet ready to develop enterprise Applications on Linux but I think it will be feasible in coming days.