/ mono

ASP.NET vNext on Linux

In this series of posts I dig into how to create a webapi application using .net and the vNext asp.net running under mono on a linux machine.

Environment

Ubuntu 14.04 or Mint 17

Contents

  1. Installation/Setup
  2. DNVM
  3. DNX
  4. Libuv
  5. Yeoman
  6. Gulp
  7. Bower
  8. Create Hello World
  9. Middleware
  10. Adding Response Headers
  11. Adding Request Headers
  12. Dependency Injection

Tutorial Requirements

  1. DNVM
  2. Node and NPM
  3. VS Code

Setup

Install DNVM
curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvminstall.sh | DNX_BRANCH=dev sh && source ~/.dnx/dnvm/dnvm.sh
Install DNX for .NET Core
sudo apt-get install libunwind8 gettext libssl-dev libcurl3-dev zlib1g libicu-dev

dnvm upgrade -r coreclr
Install libuv-dev

libuv is used by the webhosting infrastruction in Kestrel (which uses OWIN).
One thing that I found when doing dnx web is that it complained about libuv not being on the system. This didn't seem to be spelled out in any of the source documents I was using to figure this stuff out with

sudo apt-get install libuv-dev
Setup Yeoman, Gulp, and Bower

These tools are used to bootstrap a new project. This would be similar to doing a new project from Visual Studio.

sudo npm install -g yo generator-aspnet gulp bower

Hello World

For this example we're going to use npm and yeoman to do all of the twiddly heavy lifting. If you don't have node.js installed, you'll need to do that before the rest of this tutorial will work

Now Create your first app
mkdir -p ~/projects
cd ~/projects
yo aspnet

You should see something to the effect of

For this exercise just select "Web API Application"
And give your project a name

Finally you should see the following output

Follow the instructions above, gets you to this output

$ dnx web
Hosting environment: Production
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

By default the Web Api applications creates a sample api controller that covers api/values. So you can test that everything is working by using curl

curl http://localhost:5000/api/values

or if you have JQ

curl http://localhost:5000/api/values | jq .

Response

[
  "value1",
  "value2"
]

Middleware

Up till now all we've really been is a glorified meat bot for the various command line things to actually get to the point of having a running project.

Dependency Injection

This is a subject that deserves it's own post. If you find yourself scratching your head about what this even means. Use your favorite search engine and look for "Dependency Injection", "IOC", or "Inversion of Control". It's a great pattern when understood and applied correctly.

But in an overly small nut shell, this allows your to write and plugin your components into the asp vNext framework without having to take on the burden of controlling object creation at the right time. One additional concept is lifetime scope of your objects. Asp.net is a multi threaded system. As such you will need to make sure your keep in mind the scope in which your coded is executing under, this will prevent you from "crossing the streams" as it were aka causing Race Conditions. This really shines if you're following the group of principles in SOLID

Unfortunately even the above paragraph leaves out a lot of the hand wavy neck beard, oh yeah but in this situation, concepts. That will be on your to dig in and figure out, enjoy!

Future Articles:

In your project you might want to do various things

  1. Add Custom Response Headers
  2. [Append Request Headers]
  3. [Modifying the Response Content]
  4. [Authentication]
  5. [Dependency Injection / IOC]
  1. VS Code
  2. DNVM Install
  3. ASP.NET 5 using DNVM
  4. SOLID
  5. JQ
ASP.NET vNext on Linux
Share this