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?

  • All tasks are distributed: handy distribution of the tasks among services.
  • Decomposition problem is solved: micro service usually contains small number of lines.
  • Independence from development tools: for each service you can use the programming language which suits best for effective solution of the task.
  • 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.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.

If you would like to launch a new desktop, web or mobile application, website, improve your brand identity or maybe you have some questions, contact us at any time via email: [email protected] or skype: igor.zorich.

Thanks for reading.