Micro Service Architecture using Nancy

31 Jan 2017, By: Sanchir Kartiev


From the moment when micro service architecture appeared, people increasingly notice its similarity to service-oriented architecture (SOA). In fact SOA was ahead of time and at the height of its development wasn't so popular with software developers. Micro service architecture is a light-weight realization of SOA. The main idea of micro services is to divide a system into the number of individual services which communicate with each other via messages.

What are the advantages of micro service architecture?

1) Decomposition problem is solved: micro service usually contains small number of lines.
2) All tasks are distributed: handy distribution of the tasks among services.
3) Independence from development tools: for each service you can use the programming language which suits best for effective solution of the task.
4) Fail-safety: Micro service usage brings horizontally scalable and fail-safe code.

In order to realize micro service architecture in ASP.NET Core Nancy framework is designed which is also intended to implement Run anywhere conception. Nancy is a self-contained and it doesn't depend on any other framework.We will start with describing the way to use NancyFX to create micro service architecture for ASP.NET Core.

Firstly, we will create an ASP.NET Core project.

Choose an empty project (Figure 3).The following files are created: 

- Microservices\Program.cs, Microservices\Startup.cs, Microservices\project.json 

We need to move to packet management page Nuget to add Nancy library (Tools -> NuGet Package Manager -> Manage NuGet Packages). Don't forget to place check mark on Include prerelease as NancyFX supports ASP.NET Core only in 2 pre-release version so far.



public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
            app.UseOwin(buildFunc =>
                    buildFunc.UseNancy()
            );
}

Where app.UseOwin configures ASP.NET Core to use Owin and buildFunc.UseNancy() adds Nancy in Owin context. This allows Nancy to process input HTTP requests. Describe in the class constructor the path which shows current time in UTC:

public class CurrentDateTimeModule : NancyModule
{
        public CurrentDateTimeModule()
        {
            Get("/", _=> DateTime.UtcNow);
        }
}

We launch the project and check Postman.



The result of the request to the module showing current time in UTC. Let’s create another module which will receive requests onto the path / Now which will show the time in the current timezone.

public class CurrentDateTimeInLocalModule : NancyModule
{
        public CurrentDateTimeInLocalModule()
        {
            Get("/Now", _ => DateTime.Now);
        }
 }


In this article we considered a simple example of building ASP.NET Core application using microservices. A microservice is implemented in NancyFX with the help of Nancy Module. According to the concept it should have only one task – to deliver current time in both JSON and XML format. Latest tests show that ASP.NET Core is quite able to compete with NodeJS and GO in efficiency and act as a main framework for writing the server part of highly loaded applications.


Recent Articles and News


More articles from Mentalstack

Using Electron to Develop Cross-Platform Apps

The biggest dream of a customer is the approach Develop Once – Run Anywhere that means that you pay once for the development and get an application for three platforms at once: “Windows”, “Linux”, “Mac OS”.

How to Find a Top Web or Mobile Developer

Finding a web or mobile app development team is not that difficult, it is finding the right one for your business that can be tricky. The right partner can add value in more ways than just development, which will help you to maximize revenues for your app business. Furthermore, save you from some fuss later on, as a warranty comes with every single app they craft.

Micro Service Architecture using Nancy

From the moment when micro service architecture appeared, people increasingly notice its similarity to service-oriented architecture (SOA). In fact SOA was ahead of time and at the height of its development wasn't so popular with software developers. What are the advantages of micro service architecture?