Introduction

In this article, we are going to create microservices using Azure Service Fabric SDK. First, you'll learn how to set up the environment. Next, you'll be introduced to the Service Fabric reliable services. We will actually build two microservices, one stateless and one state-full. We will also go through options of how you are supposed to implement communication in distributed microservice scenarios, and we'll connect our microservices together.

Roadmap

  1. Building the environment

    1. Getting Visual Studio Ready
    2. Installing Azure Service Fabric SDK

  2. Introduction to Reliable Service
  3. Creating a Product Catalog
  4. Building Product Catalog Service
  5. Creating a Web API
  6. Connecting Two Services Together
  7. Test Using Postman
  8. Use Cases

Building the environment to use Azure Service Fabric

I’ll suggest you use Visual Studio 2019 where it works seamlessly.
Now, our environment is ready and we can start playing with Service Fabric.

Introduction to Reliable Service

In the Azure Service Fabric world, any process which runs in the background is called a reliable service. Therefore, these two services that we are going to build are reliable services. With reliable services, you get a simple programming model, access to Azure Service Fabric API, access to Reliable storage and a Pluggable communication model.

Creating a Product Catalog (stateful service)

After the solution is generated by Visual Studio, you can see two projects in here. The first project, Ecommerce is a Service Fabric project. Basically, it contains a description of how to deploy the application, what are the application parameters, and which services it consists of. The other project, ECommerce.ProductCatalog, is an implementation of the service. Visual Studio project generator creates one so we can get a complete, compilable, deployable, and working application. Otherwise, there will be no point, right?
Creating Services With Azure Service Fabric
Let’s explore the Program.cs file in Ecommerce.ProductCatalog service project. Simply, it does three things.
  1. It registers the Reliable Service,
  2. Log Reliable Service has started,
  3. Sleep forever.
Simple enough, right?
Creating Services With Azure Service Fabric
Building Product Catalog ServiceNow let's have a look what's inside that service class that we just registered. ProductCatalog class is the entry point to our service. Azure Service Fabric creates an instance of this service when it's deployed and passes a service context. The context can be used to access various information about the service environment. CreateServiceReplicaListeners is called by Azure Service Fabric to create communication and points for your service.
Creating Services With Azure Service Fabric
And RunAsync is called when your service is started. This is where you can run code which executes in the background or perform some heavy initialization logic.
Creating Services With Azure Service Fabric

Building Product Catalog Service

Back to our solution in Visual Studio, and the first thing I'm going to do is create another project of class library type. I'll call it ECommerce.ProductCatalog.Model, and the purpose of it will be store classes shared between microservices.
That's how simple it is to store the data in Azure Service Fabric. Now that we've created product catalog stateful service, let's get to building the front-end service, which allows external users to use our system.