BLOGs
Technology
Published November 28th, 2016 by

Using NancyFx in Asp.Net

Share on FacebookShare on LinkedInTweet about this on TwitterShare on Google+Pin on Pinterest

nancyfx

This post explains the concept of NancyFx framework. Asp.net development experts have shared the major advantages of this framework and reasons to use it in asp.net. Read further to know about the NancyFx framework and its advantages. You will also learn about the difference between asp.net mvc and NancyFx.

Introduction:

NancyFx is a framework for creating Http services on Asp.Net and mono. It is low-ceremony and lightweight. The main aim of NancyFx is to remain out of way and provide proper path for all interactions. It main advantage is that it supports GET, PUT, PATCH, HEAD, DELETE, OPTIONS, PATCH and PUT requests. It supports conventions rather than configurations. Using NancyFx, it is possible to return the response with few keystrokes.

Advantages of NancyFx:

  1. NancyFx is created using the dependency injection.
  2. Easy to setup: NancyFx setup does not require any configuration. It is simple to set up and customize NancyFx.
  3. It can run anywhere and it is host-agnostic. It can execute on WCF, IIS, and EXE or in an application that is self-hosted.

Build To Run Anywhere:

NancyFx does not have any dependency on the existing frameworks and can run anywhere. It has its own response and request objects. One of the most important concepts of Nancy is hosts. Host works like an adapter for Nancy and hosting environment, allowing NancyFx to run on technologies like OWIN, WCF, Asp.Net etc.

Implementations of hosts are not provided with the framework of NancyFx.  Like other functionalities, the hosts are shipped separate from the NancyFx framework. Creating an application on NancyFx is similar to choosing favorite parts from buffet of web frameworks. The minimum requirement of NancyFx is the host and the framework.

The Super-duper Happy Path:

One of the main requirements in most of the APIs is to provide a Super-super happy path, also known as SDHP. The ethos of NancyFx is SDHP.

It is not easy to describe about it exactly but the main ideas are as below:

  1. It simply works: It does not require any configuration. Things are very easy to pick up and do not require any mucking. Created a module? It is automatically discovered. Added a View Engine? Everything is easy to go and you do not need to do anything else. Even if a new dependency is added to your module, NancyFx will automatically find that and will inject for you. There is no need of any configuration.
  2. Easy to Customize: NancyFx is very easy to customize.
  3. Low Ceremony: The Nancy Code required in the application should be less. The main advantage of using NancyFx is the code. The code goes out of the way and allows programmers to create wonderful applications easily. It is very easy a running Nancy application in a single Tweet.
  4. Low Friction: When an API is created using Nancyfx, it does not comes in your way. Rather it helps in going where you want to go.

It is fun and pleasure to create an application using NancyFx.

Using NancyFx in Visual Studio:

It is easy to create an application using NancyFx. The application is created with the assumption that the developer is using Visual Studio 2010 and Nuget is already installed. The application will also run perfectly on MonoDevelop and Mono. Let us create a ‘ Hello World’ application using NancyFx and its hosting.

  1. In case Visual Studio 2012 or greater is used, it is required to install the pack of SideWaffle Template for Visual Studio. Templates of Nancy project needs to be installed by the users of Visual Studio 2010.
  2. In case SideWaffle template is used, add a new project of Nancy with Asp.Net host. In case of Nancy project templates, add a new Nancy Web Application of Asp.net hosting type.
  3. Create a module of NancyFx that is a normal class of C #. It will define a handler for the application’s root URL by inserting few lines of code to constructor.
  4. Compile the application and run it.
  5. It is recommended to make use of the Nuget to look for any updates.
public class HelloModuleExam: NancyModulExam

               {

                 public HelloModuleExam ()

                     {

                        Get ["/"] = parameters => "Hello World Example";

                      }

                   }

The module should be declared as public; otherwise NancyFx will not be able to find the module.

Difference between NancyFx and Asp.Net MVC:

The main difference between the Asp.Net MVC and NancyFx is regarding the availability of tooling. There are many available project templates in Asp.Net MVC that are very useful for the beginners. However, these templates can come in the way of experienced programmers.

One concern is that most of the Asp.Net MVC documentation makes use of the templates that are pre-configured. Because of this, developers have poor knowledge of the framework. NancyFx is basically akin to Nodejs like frameworks. Nancy is lightweight and its core is very easy.

All tools required for Asp.Net MVC and Web APIs are available in all Web projects. As an example, it is possible to scaffold Web API controllers and MVC in any project and not only Web API or MVC projects.

Another difference lies in the way of the route definition for both Asp.net MVC and NancyFx. The route definitions in Asp.net MVC are different from the Actions invoked by the Controllers. Whereas these are inline in NancyFx.

Will Asp.Net Core Replace Nancy

It is possible to run NancyFx on Asp.Net Core. Dotnet core is a complete runtime for Asp.Net web applications whereas NancyFx is simply a micro framework for applications.

Infact Nancy is the Web framework that should be recommended if it is required outside of MS stack. The code on Nancy will run without any modification irrespective of the platform or the hosting model used.

All the content shared in this post belongs to the author of an asp.net development company. If you wish to share your thoughts regarding the NancyFx framework, comment below.

Johnny Morgan

Technical Writer at Aegis Infoways
Johnny Morgan as a technical writer at Aegis Infoways since more than 5 years. I write articles especially for Java, Python and Asp.Net. I have also got well response to write articles on CRM, Hadoop and QA.

Latest posts by Johnny Morgan (see all)

Our rankings are completely independent, transparent, and community driven; they are based on user reviews and client sentiment. These technology companies had to earn their way up and didn't just pay their way up.

View Rankings of Best Technology Companies