Levvel Blog - Modern .NET – Part III

Modern .NET – Part III

Update Nov 18, 2015 - VS Code is now open source, and .NET Core & ASPNET 5 are now in release candidate status. Now we can use what we have learned in production.

Welcome to the last part in our three part series on modern .NET development. So far we have introduced the ecosystem and we have covered open source .NET. In this post, we will go through the new toolchain for a modern .NET development. At this stage we know that we can install .NET (or Mono) on Windows, Linux, or OSX. Remember from our second post that Mono is a full implementation meeting the .NET specification, and .NET Core is a modern, high quality, partial implementation of .NET. Since Mono has been covered extensively, we will focus on .NET Core and ASP.NET 5 in this post. I will be using the following tools:

  • OS X
  • Homebrew
  • .NET Core
  • ASP.NET 5
  • Atom
  • iTerm
  • zsh

First things first, lets install .NET Core and ASP.NET 5 on our OS X platform. If we follow the directions from the ASP.NET vNext documentation, we must install the .NET version manager, or dvnm.

curl -sSL https://raw.githubusercontent.com/aspnet/Home/dev/dnvm

I did receive a message stating that Type ‘source /Users/elaforc/.dnx/dnvm/dnvm.sh’ to start using dnvm. In our command above we have of course already run it, so at this point you should be able to run dvnm and see the following

.NET Version Manager - Version 1.0.0-rc2-15539
By Microsoft Open Technologies, Inc.
DNVM can be used to download versions of the .NET Execution Environment and manage which version you are using.
You can control the URL of the stable and unstable channel by setting the DNX_FEED and DNX_UNSTABLE_FEED variables.

Current feed settings:
Default Stable: https://www.nuget.org/api/v2
Default Unstable: https://www.myget.org/F/aspnetvnext/api/v2
Current Stable Override: 
Current Unstable Override: 
Use dnvm [help|-h|-help|--help] to display help text.

Now that we have installed dvnm, we need to install a .NET execution engine, or dnx. We will do this through Homebrew. Once you have homebrew installed, please run

brew update
brew install icu4c

This will update your current brews, and install icu4c, a library for Unicode support. After we have installed our prereqs, please install dnx by running

dnvm upgrade -r coreclr

Note: You can also use dvnm to install mono, but we are going to focus on coreclr.

So now if we run dnvm list we should see something like

Active Version              Runtime Architecture OperatingSystem Alias
------ -------              ------- ------------ --------------- -----
*      1.0.0-beta8          coreclr x64          darwin          d

That is it, we have now successfully installed .NET Core and CoreCLR on OSX. This will allow us to build a sample ASP.NET 5 application.

We will use a Yeoman generator to scaffold a quick and dirty application. Please install by running npm install -g generator-aspnet. Note: I am running node v4.2.1 and npm v2.14.7 at the time of this writing.

Now lets create a directory for our new modern .NET application – mkdir ~/code/modern-dotnet && cd ~/code/modern-dotnet. Go ahead and make a src directory in our new solution folder as well – mkdir ~/code/modern-dotnet/src. At this point I am going to switch to Atom for my modern .NET development (go ahead and pick up the Omnisharp Atom package as well). I am a big fan of IDEs and I also highly recommend Visual Studio Code, but in this case I just want a simple text editor with vi keybindings (unfortunately at the time of this writing Code does not have a vi keybindings plugin).

Please create a global.json in the root modern-dotnet directory with the following contents

{
  "sdk": {
    "version": "1.0.0-beta8"
  }
}

Make sure that the version matches the output of dnvm list. Now we can run yo aspnet under your src folder and generate our first ASP.NET 5 application on OS X. I selected a Web API project and named the project modern-dotnet. This created the following output:

[?] What type of application do you want to create? Web API Application
[?] What's the name of your ASP.NET application? (WebAPIApplication) modern-dotn[?] What's the name of your ASP.NET application? modern-dotnet
create modern-dotnet/.gitignore
create modern-dotnet/Dockerfile
create modern-dotnet/Startup.cs
create modern-dotnet/project.json
create modern-dotnet/Properties/launchSettings.json
create modern-dotnet/Controllers/ValuesController.cs
create modern-dotnet/wwwroot/README.md
create modern-dotnet/wwwroot/web.config

Your project is now created, you can use the following commands to get going
cd "modern-dotnet"
dnu restore
dnu build (optional, build will also happen when it's run)
dnx web

In our root directory, go ahead and run dnu restore. This will install all the needed NuGet packages, just like npm or other package managers. Next lets launch our sample Web API by running dnx web. You should see the following:

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

At this point lets open our browser or curl to http://localhost:5000/api/values and we can now see our modern .NET web api is working.

["value1","value2"]

Woot! So how is this working? Specifically we are now running a minimum Web API project. The files include:

  • project.json - Dependency management. This one defines the core .NET libraries needed. Note that we are running on the Kestrel web server.
  • Startup.cs - The basic application initialization, pulls in logging, MVC, and static file usage for the web server.
  • Controllers/ValuesController.cs - The actual code.

There are of course other files, but these are enough to understand and get started.

This will conclude our tour of the modern .NET development platform. We have learned a little history, and the community has changed significantly over the last 5 years. We explored the open source framework options and how Microsoft is making big strides in their open source offerings. Finally, we dove into the tool chain and created an ASP.NET 5 Web API on OS X. This is something that was unheard of a few years ago, but now has editors, plugins, command line tooling, and embedded web servers. The community has really stepped up and provided for these new environments and we can all benefit from these modern development practices.

Please check out the ASP.NET 5 documentation for how to take next steps.

The Levvel team can help you:

  • Modernize your legacy software
  • Train your development staff on modern development practices
  • Accelerate development efforts and reduce operational expenses with open source software

Related Posts