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.
Ubuntu 14.04 or Mint 17
- Create Hello World
- Adding Response Headers
- Adding Request Headers
- Dependency Injection
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
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
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
or if you have JQ
curl http://localhost:5000/api/values | jq .
[ "value1", "value2" ]
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.
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!
In your project you might want to do various things
- Add Custom Response Headers
- [Append Request Headers]
- [Modifying the Response Content]
- [Dependency Injection / IOC]