ASP.NET 5 (MVC6) Ground Up #0 - A segue into OS X

This is a sidestep from the Ground Up Series to help get you up and running on OS X as opposed to Windows. You will still want to read Part 1 as only the additional steps required will be covered here.

Also, I'm a complete amateur of OSX. Please keep that in mind. This has been run on a fresh Yosemite install.

First step, we need a package manager to help us install the dependencies.

Homebrew - "The missing package manager for OS X"

You can think of Homebrew in the same light as chocolatey / oneget (Windows) or apt-get (*nix) . It's a simplified way of getting the bits on to your machine.

Install Homebrew by following the steps outlined on their site. Repeated here:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  

Homebrew has it's own repository of packages but as things are still in beta, ASP.NET 5 hasn't yet made it's way into the main repository. Luckily we can get around that.

Tapping into ASP.NET

Homebrew has the concept of adding a tap - effectively a way of sideloading your own Homebrew package by adding Github repositories.

brew tap ctolkien/k  

The naming convention here is going to reference the repository: https://github.com/ctolkien/homebrew-k

This repository is my fork of the main aspnet/homebrew-k repository which has been configured to load the beta2 KRE on Mono (currently, the aspnet repo is loading beta3).

You can now install the K Version Manager with:

brew install kvm  

As part of this process, it will also run kvm upgrade, fetching you the latest version of the KRE. Finally, to be able to run the kvm commands:

source kvm.sh  

Which will load the functions from kvm.sh into your current shell process.

Kestrel - the cross platform web/app server

Kestrel is a development web server built on top of libuv which underpins Nodejs. For those familiar with Windows, think of it as a cross platform IIS Express.

Crucially, it runs on OS X, Linux and Windows. This makes it a viable option for development regardless of the operating system you or your colleagues are working on.

Given time, this will also get the rubber stamp from MS to use as a supported production application server. Typically you'll want to stick with IIS on Windows, but Kestrel could be paired with Apache or Nginx on other platforms for production scenarios.

Now that we've got the runtime installed the next (and frankly, only) difference from Part 1 is booting a web server.

We'll start with our base "Windows" repository as it was at the end of Part 1 (note that I'll be trying to keep a cross platform version in sync in the xplat branch on Github).

git clone https://github.com/ctolkien/vnext-ground-up  

And then change directory into /part1

Now we can start adding what we need to run on another platform. Let's install Kestrel using the K Package Manager.

kpm install Kestrel 1.0.0-beta2  

Note: Casing is important: This is likely to be addressed but as OS X runs on a case sensitive file system, you must capitalise Kestrel so it exactly matches the package name.

This command will download the package from Nuget.org and update your project.json file with the Kestrel dependency.

Revisiting Commands

In our Part 1 Windows Version, we had a web command that we used to kick things into gear.

This used AspNet.Hosting and passed the request off to WebListener. This time around, we still use AspNet.Hosting but we're going to pass the requests off to Kestrel.

We need to add a command to run our Kestrel server. Add the following command to project.json under the existing web command.

"kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5001"

Our complete project.json is now comprised of:

{
  "dependencies": {
    "Microsoft.AspNet.Hosting": "1.0.0-beta2",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-beta2",
    "Kestrel": "1.0.0-beta2"
  },
  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000",
    "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5001"
  }
}

So we now have two distinct commands, web and kestrel. Both of these are executed the same way, and they both do the same thing but hand off the requests to different processes.

Finally, run:

k kestrel  

We're now up and running on OS X. Browse to http://localhost:5001 to see Hello World!

There is an issue currently where it's not easy to restart the server once it's been booted up. Checkout the KMON project. This is built on top of NodeMon and will watch your files for changes and automatically restart the server once it's detects a change.

That's nice but the experience in Sublime / Vim / Emacs / Foo sucks!

OmniSharp to the rescue! OmniSharp is a community lead project to bring a first class .NET experience into your editor of choice. Join in with the guys/gals on Jabbr.


ASP.NET 5 running across different platforms is to my mind one of the most exciting developments that the .Net space has had in years. Hopefully this has helped you get up and running.

Special thanks to @Pure_Krome for reviewing and @Frankr (both on Jabbr.Net) for prodding me to write this down.

comments powered by Disqus