共用方式為


Developing Web Applications using ASP.NET 5 (beta 7) on Ubuntu Linux 14.04.2 LTS Part 1 – Installing & Configuring

By: Nestor Guadarrama

 

With the new version of ASP.NET 5, web developers requires change their web development paradigms working on ASP.NET. This version was totally redesign in order to let ASP.NET being more open. This means basically that now, ASP.NET 5 is considered a new open-source and cross-platform framework for building modern cloud-based Web applications using .NET.

 

Microsoft built it from the ground up to provide an optimized development framework for apps that are either deployed to the cloud or run on-premises. It consists of modular components, so you retain flexibility while constructing your solutions. You can develop and run your ASP.NET 5 applications cross-platform on Windows, Mac and Linux, because ASP.NET 5 is fully open source!

 

In summary, with ASP.NET 5 you gain the following foundational improvements:

 

· New light-weight and modular HTTP request pipeline

· Ability to host on IIS or self-host in your own process

· Built on .NET Core, which supports true side-by-side app versioning

· Ships entirely as NuGet packages

· Integrated support for creating and using NuGet packages

· Single aligned web stack for Web UI and Web APIs

· Cloud-ready environment-based configuration

· Built-in support for dependency injection

· New tooling that simplifies modern web development

· Build and run cross-platform ASP.NET apps on Windows, Mac and Linux

· Open source and community focused

image

Figure 1. New Features on ASP.NET 5

 

.NET EXECUTION ENVIRONMENT (DNX) & .NET Core

 

ASP.NET 5 applications are built and run using the new .NET Execution Environment (DNX). Every ASP.NET 5 project is a DNX project. ASP.NET 5 integrates with DNX through the ASP.NET Application Hosting package.

The .NET Execution Environment (DNX) is a software development kit (SDK) and runtime environment that has everything you need to build and run .NET applications for Windows, Mac and Linux. It provides a host process, CLR hosting logic and managed entry point discovery. 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.

 

DNX provides a consistent development and execution environment across multiple platforms (Windows, Mac and Linux) and across different .NET flavors (.NET Framework, .NET Core and Mono). With DNX you can develop your application on one platform and run it on a different platform as long as you have a compatible DNX installed on that platform.

 

image

Figure 2. New ASP.NET architecture and modularity

 

DNX uses a small, optimized runtime that can be targeted by any .NET application called .NET Core 5. .NET Core 5 is a modular runtime and library implementation that includes a subset of the .NET Framework. Currently it is feature complete on Windows, and in-progress builds exist for both Linux and OS X. .NET Core consists of a set of libraries, called “CoreFX”, and a small, optimized runtime, called “CoreCLR”.One of the key benefits of .NET Core is its portability. You can package and deploy the CoreCLR with your application, eliminating your application’s dependency on an installed version of .NET (e.g. .NET Framework on Windows). You can host multiple applications side-by-side using different versions of the CoreCLR, and upgrade them individually, rather than being forced to upgrade all of them simultaneously.

 

INSTALLING AND CONFIGURING ASP.NET 5 ON UBUNTU

 

For the purpose of this articles’ series, I’m using a Hyper-V VM with Ubuntu Desktop 14.04.2 LTS:

image

Figure 3. Ubuntu Desktop 14 VM

 

You can install ASP.NET 5 using command line shell on your Ubuntu desktop or you can use a Docker image. I’ll go for using command lines instead.

 

Step 1: Get your system ready

 

Now, in order to proceed install and configure ASP.NET packages and requirements, please be sure that your environment is running with latest updated packages. Execute apt-get update for this purpose:

 

sudo apt-get update sudo apt-get upgrade

 

This command will update all packages already installed on your environment.

 

Step 2: Install .NET Execution Framework (DNX) / Mono

 

According with ASP.NET documentation about how to install ASP.NET 5 on Linux environment, process needs to follow next sequence:

 

1. Install unzip

2. Install Curl

3. Install .NET Execution Framework (DNX), or Install .NET DNX for Mono

4. Optional: Intall MonoDevelop

5. Install libuv

6. Download and install DNMV (.NET Version Manager)

7. Add sources to NuGet.config

 

One short comment about .NET Execution Framework / .NET DNX for Mono. Because now developers needs to choose between which version of DNX developer will use for create code, is important to understand the three (3) main scenarios for use DNX: a) .NET Framework, b).NET Core and, c) Mono

 

· .NET Framework: The .NET Framework is the most well-known and mature of the three options. The .NET Framework is a mature and fully featured framework that ships with Windows. The .NET Framework ecosystem is well established and has been around for well over a decade. The .NET Framework is production ready today and provides the highest level of compatibility for your existing applications and libraries.

 

The .NET Framework runs on Windows only. It is also a monolithic component with a large API surface area and a slower release cycle. While the code for the .NET Framework is available for reference it is not an active open source project.

 

· .NET Core: NET Core 5 is a modular runtime and library implementation that includes a subset of the .NET Framework. Currently it is feature complete on Windows, and in-progress builds exist for both Linux and OS X. .NET Core consists of a set of libraries, called “CoreFX”, and a small, optimized runtime, called “CoreCLR”. .NET Core is open-source, so you can follow progress on the project and contribute to it on GitHub.

 

The CoreCLR runtime (Microsoft.CoreCLR) and CoreFX libraries are distributed via NuGet. Because .NET Core has been built as a componentized set of libraries you can limit the API surface area your application uses to just the pieces you need.

 

The API factoring in .NET Core was updated to enable better componentization. This means that existing libraries built for the .NET Framework generally need to be recompiled to run on .NET Core. The .NET Core ecosystem is relatively new, but it is rapidly growing with the support of popular .NET packages like JSON.NET, AutoFac, xUnit.net and many others.

 

· Mono: Mono is a port of the .NET Framework built primarily for non-Windows platforms. Mono is open source and cross-platform. It also shares a similar API factoring to the .NET Framework, so many existing managed libraries work on Mono today. Mono is not a supported platform by Microsoft. It is however a good proving ground for cross-platform development while cross-platform support in .NET Core matures.

 

However, from ASP.NET documentation page on Github, for ASP.NET 5 beta 7, there are a large list of scenarios that are not ready for build applications using 4.5.1 & 4.6 as target versions for .NET Core, so on this article, I’ll be working with Mono option.

 

2.1 Installing unzip

 

Under normal conditions, your Ubuntu image should has already installed unzip package. However, if this is not your case, you can install unzip using next command:

 

sudo apt-get install unzip

 

2.2 Install Curl

 

Curl is a command line tool for transferring data specified with URL syntax. In order to install Curl, you can use next command:

 

sudo apt-get install curl

 

2.3 Download and Install Mono

 

Mono is how .NET applications can run on platforms other than Windows. Mono is an ongoing effort to port the .NET Framework to other platforms. In the process of developing ASP.NET 5 we worked with the Mono team to fix some bugs and add features that are needed to run ASP.NET applications. These changes are only in builds of mono that are greater than 4.0.1.

 

At the moment of write this article, last stable version of Mono in Ubuntu is 4.0.4, so we will have to pull it directly from the Mono repositories:

 

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF echo "deb https://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list sudo apt-get update sudo apt-get install mono-complete

 

You can test your Mono installation using next command:

 

Mono -V

 

image

Figure 4. Testing Mono installation

 

2.4 Optional: Install MonoDevelop

 

Although is not required for this article, I thought that it would be valuable to show how to install MonoDevelop (after being installed Mono compiler).

 

MonoDevelop enables developers to quickly write desktop and web applications on Linux, Windows and Mac OS X. It also makes it easy for developers to port .NET applications created with Visual Studio to Linux and Mac OS X maintaining a single code base for all platforms.

 

Installation process is also very straight forward. Follow next commands:

 

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF echo "deb https://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list sudo apt-get update

 

Because (I’m assuming that you already completed step 2.3) you already have installed Mono compiler (mono-complete package), once you complete and update your environment, from Terminal you can execute monodevelop:

 

image

Figure 5. Testing MonoDevelop installation

 

2.5 Download and install libuv

 

Libuv is a multi-platform asynchronous IO library that is used by the KestrelHttpServer that we will use to host our web applications.

 

To build libuv you should use following commands:

 

sudo apt-get install automake libtool curl -sSL https://github.com/libuv/libuv/archive/v1.4.2.tar.gz | sudo tar zxfv - -C /usr/local/src 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

 

NOTE: make install puts libuv.so.1 in /usr/local/lib, in the above commands ldconfig is used to update ld.so.cache so that dlopen can load it. If you are getting libuv some other way or not running make install then you need to ensure that dlopen is capable of loading libuv.so.1

 

2.6 Download and install DNMV (.NET Version Manager)

 

Download and install DNMV using next command:

 

curl –sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh

 

Once this step is complete you should be able to run dnvm and see some help text.

 

image

Figure 6. Testing .NET Version Manager

 

2.7 Add Sources to NuGet.config

 

Now that we have DNVM and the other tools needed to run an ASP.NET application we need to add the development configuration sources to get nightly builds of all the ASP.NET packages.

 

To add this to your package sources you need to edit the NuGet.config: (I’ll use Gedit for edit this file)

 

cd ~/.config/NuGet/ gedit NuGet.config

 

Add, next section to NuGet.config file

 

<?xml version="1.0" encoding="utf-8"?> <configuration>   <packageSources>     <add key="AspNetVNext" value="https://www.myget.org/F/aspnetvnext/api/v2/" />     <add key="nuget.org" value="https://www.nuget.org/api/v2/" />   </packageSources>   <disabledPackageSources /> </configuration>

 

image

Figure 7. Editing NuGet.config file

 

Step 3: Creating my first ASP.NET 5 from scratch

 

Most of all examples that you will find on the web, are cloning ASP.NET repository from GitHub. Instead, I wanted to create something from scratch, so I decided use Visual Studio Code on Ubuntu for testing create new ASP.NET vNext apps:

 

3.1 Download and Install Visual Studio Code

 

Download and install process for Visual Studio Code is very straight forward:

 

· Download VSCode-linux64.zip from Visual Studio Code.

· Extract .zip file in a folder

· Double click on Code application

 

In order to follow the same approach that I’ve been using on this article, I used ubuntu-make (Ubuntu Developer Tools Center). Ubuntu Make is a command line tool which allows you to download the latest version of popular developer tools on your installation, installing it long side all the required dependencies (which will only ask for root access if you don't have all the required dependencies installed already), enable multi-arch on your system if you are on a 64 bit machine, integrate it with the Unity launcher and other cool features.

 

So, using my terminal window, I follow next commands:

 

sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make sudo apt-get update sudo apt-get install ubuntu-make

 

Once Ubuntu Make is installed, run the command given below to install Visual Studio Code on Ubuntu System:

 

umake web visual-studio-code

 

NOTE: At this point, I was using Ubuntu MATE as my desktop environment. However, Visual Studio Code doesn’t work correctly with this Desktop environment, so I changed for my original Gnome Ubuntu Desktop

At this point, with a double click on Code application, Visual Studio Code will open:

 

image

Figure 8. Visual Studio Code already installed and running

 

3.2 Testing Visual Studio Code

 

ASP.NET 5 introduces several new fundamental concepts of web programming that are important to understand in order to productively create web apps. ASP.NET 5’s project structure adds new concepts and replaces some legacy elements found in previous versions of ASP.NET projects.

 

ASP.NET 5 applications are defined using a public Startup class:

 

image

Figure 9. Startup class structure

 

The ConfigureServices method defines the services used by your application and the Configure method is used to define what middleware makes up your request pipeline.

 

Also, ASP.NET 5 application requires a configuration file called project.json. The project.json file is new to ASP.NET 5. It is used to define the project’s server side dependencies (discussed below), as well as other project-specific information:

 

image

Figure 10. project.json structure

 

I won’t detail every single section on this file, because these sections will be covered in next articles. In fact, there are tons of new material to consume in order to understand correctly all new implementations. All this options will be covered later. Coming back to our testing project, using Visual Studio Code, I’ll create 2 files: project.json and startup.cs. Steps for creating a simple structure next:

 

· Create a folder where you will store files to be created.

· Create files config.json and startup.cs

· Test web app

 

For the first step, I created a folder called testproject. Then, I opened Visual Studio Code and opened the same folder to start working on it:

 

image

Figure 11. Creating and selecting testproject folder on Visual Studio Code

 

Now I’ll create project.json and startup.cs files into this folder:

 

image

Figure 12. Creating and selecting testproject folder on Visual Studio Code

 

One interesting feature in Visual Studio Code is a rich intellisense on all dependency-based modules and files related to .NET Core. Once you start working on your code, .NET Core and DNVM will feed Visual Studio Code with all possible options/combinations for it. Moving forward on this file, I’ll write next structure into project.json:

 

{     "version": "1.0.0-*",     "webroot": "wwwroot",     "tooling": {         "defaultNamespace": "FirstAspNet5Test”     },     "exclude": [         "wwwroot",         "node_modules",         "bower_components"     ],     "publishExclude": [         "**.user",         "**.vspscc"   ],     "dependencies": {         "Microsoft.AspNet.Server.Kestrel": "1.0.0-*",         "Microsoft.AspNet.Diagnostics": "1.0.0-*",         "Microsoft.AspNet.IISPlatformHandler": "1.0.0-*"     },     "commands": {         "web": "Microsoft.AspNet.Server.Kestrel --server.urls https://localhost:6001"     },     "frameworks": {         "dnx451": { },         "dnxcore50": { }     } }

 

Once you try to save your project.json file, Visual Studio Code detects that there are several unresolved dependencies, so Visual Studio Code resolve these dependencies for you:

 

image

Figure 13. Resolving dependencies on Visual Studio Code

 

Now, is time for write some lines of code on Startup.cs file:

 

image

Figure 14. startup.cs file

 

Once you create your class, is time to test our web application. You can testing it using a couple of options:

 

From Terminal: From folder testproject, you can use next commands:

 

dnx web

 

We use “web” as a command, because we defined this “command” on our project.json file. This is how it should looks like, once you run this command:

image

Figure 15. Kestrel server running

 

From Visual Studio Code: Using command palette (Ctrl+Shift+P), you can run directly command dnx:

 

image

Figure 16. Command Palette option in Visual Studio Code

 

Finally, you can test your web application using your default browser:

 

image

Figure 17. Testing ASP.NET 5 web application

 

On the next articles, I’ll be testing some interesting new features on ASP.NET 5.

 

 ‘till next time!

 

References:

 

· Introduction to ASP.NET 5

· Introducing .NET Core

· DNX Overview

· Choosing the Right .NET For You on the Server

· Getting Started with ASP.NET 5 and DNX

· Install Mono on Linux

Comments

  • Anonymous
    October 18, 2015
    This series going to be very interesting !!
  • Anonymous
    October 18, 2015
    This series going to be very interesting !!