ASP.NET Core Series: NancyFX

ASP.NET 5 NancyFXLast week I decided to start migrating one of my existing ASP.NET application that uses NancyFX over to ASP.NET Core.  The process is actually pretty straight forward if you are familiar with OWIN.

OWIN

OWIN defines a standard interface between .NET web servers and web applications. The goal of the OWIN interface is to decouple server and application, encourage the development of simple modules for .NET web development, and, by being an open standard, stimulate the open source ecosystem of .NET web development tools.

To clarify and reword slightly, OWIN is a open source specification that is not defined by Microsoft and is not an actual implementation.

ASP.NET Core supports OWIN and defines middleware to be used in a  request pipeline with the Microsoft.AspNet.Owin package.

ASP.NET Core NancyFX Demo

All of the source code for this demo is available on GitHub.

The first thing we are going to do for this demo is add the appropriate nuget packages.

Note: At the time of this post, ASP.NET Core is curently at RC1, which is still a pre-release and therfore you will need to include the “-Pre” option when installing any pre-release packages.

PM> Install-Package Microsoft.AspNet.Owin -Pre

PM> Install-Package Nancy

PM> Install-Package Nancy.Owin

Now in our Configuration method of our Startup class, there is a new extension method of UseOwin provided by the Microsoft.Asp.Net.Owin package, which allows us to include additional OWIN compatible middleware to our ASP.NET Core pipeline.

The Nancy.Owin package also provides an extension method UseNancy that we can use inside our UseOwn method.

For the demo, I’ve created a simple Nancy Module.  Since Nancy will find it automatically (which is really nice) there is no registration needed.

Nancy does not support targeting .NET Core however does support Mono.  You will have to remove the “dnxcore50” from frameworks section of the project.json

At this point, you are ready to run the demo.  Your application should be hosted on port 5000.  Calling http://localhost:5000/nancy/demo using Postman produces the expected output.

NancyFX

Owin Performance

The ASP.NET Core documentation notes that you should not use multiple calls to UseOwin.  Rather, insert all of your OWIN middleware grouped together within one UseOwin call.

Source Codegithub-octocat

The source code for this demo is available on Github.

ASP.NET Core Series

ASP.NET Core Series: The Basics

ASP.NET 5I’ve been wanting to create a ASP.NET Core Overview and series of blog posts on ASP.NET Core, but due to the number of betas and changes that came with those, I decided to hold off.

Although ASP.NET at the time of this post is still at RC1 (update 1) and not RTM yet, there likely may be some naming changes.  Still, I think we are at a stable enough point where you can take this series and get a good feeling about where ASP.NET currently stands and how you get started writing your own apps.

Series

This series will focus on development of ASP.NET Core applications using:

ASP.NET Core

ASP.NET Core is a new open-source and cross-platform framework for developing web applications in .NET.  It is a totally new version of ASP.NET built from the ground up.

Getting things straight

Before we jump to installation and getting a new project setup, I think it’s important to define some the pieces to the puzzle, as I was pretty confused early on.  Even if you are familiar with ASP.NET 4 and are using MVC or WebAPI now, a lot has changed.

.NET Execution Environment (DNX)

The DNX is a SDK and Runtime environment that enables you to build and run .NET applications cross platform (Windows, Linux, and Mac).  It does so by providing a standard execution environment across different flavors of .NET.  Flavors?  Yes. Flavors.

If you are primarily currently running and developing in Windows, then you are using the full “.NET Framework”.  However, you can also target Mono or .NET Core

Mono

Mono is an open source port of the .NET Framework sponsored by Xamarin.  Beyond the runtime environment supported by the DNX, it also includes a C# complier and Base Class Libraries compatible with the full .NET Framework.

.NET Core

Similar to Mono, this is a new open source runtime (CoreCLR) and libraries (CoreFX) that is cross platform.    The main benefit to .NET Core because of its modularity, is the ability to deploy and run a .NET Core application on a target host without the need to have the .NET Framework installed.  This also allows you to run different applications targeting different runtimes on the same host side by side.

Why?

I do believe there has been a bigger push by Microsoft and the community (hence this post) to reach out to developers that live in other ecosystems to advocate the use of .NET and ASP.NET Core.

With Visual Studio Code and Omnisharp, there is now a legitimate effort being put forward to create cross platform tooling outside of Windows where you can developg .NET and ASP.NET Core applications.

There is also a lot of effort being put into giving developers a incredibly easy path to being developing using ASP.NET Core applications.

Previously, if you were going to write an application in .NET, you had almost no options.  Your tooling was forced to Windows and Visual Studio.  This is no longer becoming the case.  It seems the new motto is build anywhere, run anywhere.

Next Up

Next post will be taking a look at DNX a bit more in detail, how to install, and how to create a new DNX Project.

NancyFX: .NET Web Framework

NancyFXWorking in the .NET ecosystem can feel very insular.  Especially if you work in the enterprise.  At the very beginning of my career in software development (circa late ’90s), I was generally using more open tooling and languages.  The atmosphere in those communities had (past tense) a much different feeling than it does in the .NET community.

Most developers that are in .NET can feel the changes that have been occurring recently.  There has been a bigger wave of alternative and open source software awareness within .NET.  However, I still don’t think that the .NET community at large are even aware of some of the great open source libraries and frameworks.

 ASP.NET MVC / Web Api

If you were starting new web project in .NET, I think it’s safe to say that most would automatically choose ASP.NET MVC or Web Api.  There may be legitimate reasons for doing so, such as you or your team having exiting knowledge of those frameworks.

However, NancyFX is a great alternative that is very mature, simple and has great documentation.

NancyFX

Nancy is a lightweight web framework.  It’s really that simple.

At its heart it provides a really easy way to create HTTP services.  That means you could be generating and returning HTML using the MVC pattern or creating services returning JSON.  You are not forced into either or into any heavy configuration. You are simply creating endpoints using HTTP methods.

Getting Started

In my code examples, I’m going to be using my Self Host ASP.NET Demo application, which is currently demos how to use Katana (Owin) Self Host, Middleware, File Server and Topshelf.

I’m going to extend this project to include Nancy.

First let’s install Nancy from Nuget

Install-Package Nancy

For my example, I’m also going to need to Install the Nancy.Owin Package because my self host application uses the Katanta Owin Implementation with HttpListener.

Install-Package Nancy.Owin

Now that we have our packages installed, in your Startup class, use the new IAppBuilder.UseNancy() extension method.

Simplest Example

My intent for this blog post is simply to get people looking at Nancy as an alternative.  I can’t show you all all the wonderful features in one easy to digest post.  In posts to come I’m going to cover many different topics related to Nancy.  However, for this post I want to provide the simplest possible example of creating an HTTP endpoint using the GET method.

Similar to ASP.NET MVC or Web Api, you will create a class that extends another.  For Nancy, we create a class that extends the NancyModule.  It is in this class were we define the routes and the endpoint implementation.   In my example above, I’ve created a HTTP GET route to /nancy/demo.  Since my self host application is running on localhost:8080, this translates to http://localhost:8080/nancy/demo.

Here are the results:

NancyFX

As you might expect, Nancy has automatically converted my .NET string array to JSON without any configuration.

Features

As I mentioned, I plan on going over in detail many features of Nancy and why you may want to use it.  But to get you interested a bit more, here are a few key points.

  • Model Binding
  • Built in IoC Container (Support for almost all others)
  • Easy Testing
  • Content Negotiation
  • View Engines (including Razor Support)
  • Multiple Hosting Options (Katana/Owin, ASP.NET HttpHandler, more)
  • Mono Support

Please take a few moments and check out the great docs over on Github.

Source Code

GithubThe source code for this demo is available on Github.

OWIN/Katana Series

If you want more information about OWIN/Katana, please take a look at my other series of posts.

  1. How to Self Host ASP.NET Web Api

  2. Self Host ASP.NET Web Api as a Windows Service

  3. ASP.NET Self Host Static File Server

  4. How to create Katana Middleware