Tuesday, January 17, 2017

How to use NancyFx with ASP.NET Core Application

TL;DR;

In this blog post, we are going to learn how we can use Nancy framework with ASP.NET Core Application.

NancyFx Introduction:

NancyFx is a lightweight, low-ceremony framework for building HTTP based services on .NET and Mono. It is inspired by Sintara Framework for Ruby and hence Nancy was named after the daughter of Frank Sintara. Nancy Framework is a great alternative to ASP.NET APIs. It follows “Super duper happy path” phrase. It has following goals.
  • It just works – You should just use it without learning so much thing from it. Create a Nancy module and that’s it.
  • Easily Customizable – There are tons of customization available and then you can easily customize it.
  • Low-ceremony- With the minimal code you will able to run NancyFx.
  • No Configuration Required – There is no configuration required and very easy to setup.
  • Host-agnostic and Runs anywhere-  It will run on any server, self-hosted etc.
  • Low Friction- When you build software with NancyFx APIs it will help you where you want to go rather than coming in your way. 

How we can use NancyFx in ASP.NET Core:

So let’s see how we can use NancyFx in ASP.NET Core, Let’s create  ASP.NET Core API Application via File –> New project in visual studio.

create-project-nancy-api

Once you are done with creating an ASP.NET Core API application delete the controller folder and add following nuget packages.
    • Microsoft.AspNetCore.Owin: “1.0.0”
    • Nancy: “2.0.0-barneyrubble”
You can directly put  in project.json like following.

project-json-nancyfx

Now once we are done with adding packages, We need to make sure our application uses NancyFx and handles requests instead of ASP.NET MVC. So remove “app.UseMVC” in the startup.cs file and add following code in configure method.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    app.UseOwin(n => n.UseNancy());
}
Now we need to create a new Nancy module that will handle request. For this blog post, we are going to create a Home Nancy module like following.
using Nancy;

namespace NancyCoreAPI.Module
{
    public class HomeModule : NancyModule
    {
        public HomeModule()
        {
            Get("/", args => "Hello world from nancy module.");
        }
    }
}
Here in the above code, You can see that it is a standard Nancy module where you just need to write methods in the constructor and it will return text or HTML based on requirement. In our case here it will return “Hello World from nancy module” text. Once you run application browser it will look like following.

hello-world-from-nancy-module

That’s it. You can see that Even with ASP.NET Core it is very easy to use and almost no configuration required at all.

You can find complete source code of this blog post at following location on GitHub- https://github.com/dotnetjalps/CoreNancyAPI
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews