Create A MicroService In ASP.NET Core Using Azure Service Fabric

In this article, we will see how to create a Microservice in ASP.NET Core using Service Fabric and we will create a 1 node cluster in Service Fabric Local Cluster Manager and run the application in this cluster.

A microservice is a software development technique that structures an application as a collection of loosely coupled services. In a microservices architecture, services are fine-grained, and the protocols are lightweight. It decomposes an application into different smaller services improves modularity and makes the application easier to understand, develop, and test, and more resilient to architecture erosion.

A microservices architecture consists of a collection of small, autonomous services. Each service is self-contained and should implement a single business capability. 

Create a MicroService in ASP.NET Core using Azure Service Fabric 
Source:https://docs.microsoft.com/en-us/azure/architecture/guide/architecture-styles/microservices

Azure Service Fabric is a distributed system platform that makes it easy to package, deploy, and manage scalable and reliable microservices and containers. It allows developers and administrators to avoid complex infrastructure problems and focus on implementing mission-critical, demanding workloads that are scalable, reliable, and manageable.

Prerequisites for creating Microservices in ASP.NET Core,

  • Visual Studio 2017 with Azure SDK
  • Microsoft Azure Service Fabric SDK
You can download Azure Service Fabric SDK from this URL
 
To install Azure Service Fabric SDK, you must download Web Platform Installer separately.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric
 
After successful installation, you can restart the windows machine. After the system restarts, you will get a message in the system tray as shown below.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric
 
You can see the Service Fabric Local Cluster Manager in the system tray. Currently, it supports 1 and 5 node clusters. We can set up a 1 node cluster. Right click the Fabric Local Cluster Manager icon in the system tray and choose “Setup Local Cluster” -> “1 Node”.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric
 
It will take a few minutes to set up the local cluster. After some time, you will be notified with a message.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric
 
You can click “Manage Local Cluster” to open the cluster. You can get the details about the 1 node cluster.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
You can see that 5 different tabs are available. Each tab contains various information about the local cluster.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
You can also see the node in the left pane and if you click the node, you will get the details of the node. (Currently, we have only one node available).
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
We have successfully set up the Service Fabric and local cluster. Now, we can create a microservice application in ASP.NET Core.
Open Visual Studio 2017 in Administrator mode (we need some admin privileges for PowerShell later) and create a new project. Choose “Cloud” -> “Service Fabric Application” template and give a name to our project.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
Various types of Service Fabric are available. In this article, we will create a Stateless ASP.NET Core application. 
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
We can also modify the service name. By default, it is Web1.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
We can now choose the application type. Currently, I am using .NET Core 2.1 SDK. It will display all the supported templates. We will use MVC template in this article. Click OK button to proceed.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 

It will take some to create the application and download the library files to our application.

Our application contains two projects. The first project is the Service Fabric project and contains all the parameter files. Most of the files are XML files. In the script folder, one PowerShell script is there. It is used for deploying the application to a local cluster or Azure.

Create a MicroService in ASP.NET Core using Azure Service Fabric
 
The second project is a pure MVC project. Currently, it contains only one Controller and corresponding Views.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
We can build our application and run it. You will be asked to grant permission for the first time.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
You may face the following error in execution. You can ignore this error message and proceed.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
The system will automatically assign a port to our application and open in a while (I have modified the content in Index.cshtml before running the application).
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
You can see in the Service Fabric Explorer that our application is listed there.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
You can also browse the application details from the left pane.
 
Create a MicroService in ASP.NET Core using Azure Service Fabric 
 
In this article, we saw how to install Azure Service Fabric SDK and we configured a 1 node cluster locally. After that, we created a Microservice application with MVC Service using Service Fabric and saw how it is running in the cluster.