Building a Microservice with ASP.NET Core
Azure Service Fabric is Microsoft's Microservices platform and as like some other Microservices platform, it has distributed systems features, lifecycle management, independent scaling of services, rolling upgrades of applications, always-on availability, and also supports stateless and stateful services. It’s a kind of some windows components and installing these on machines will enable service fabric on the machine.
Even though Azure has first class support for Service Fabric, it can be also installed on other service providers like Amazon or even on on-premises. For the developer, there's a Service Fabric SDK which when you install on your computer gives the same experience and if you're using the Visual Studio in a simple run you can deploy your app into the cluster that's running on your local machine. In this tutorial, we are going to create an Azure Service Fabric Application with an ASP.NET Core Web API in Visual Studio.
Setting Up Dev Environment
Install Visual Studio 2017 with the Azure Development, ASP.NET and web development workloads.
Install the Microsoft Azure Service Fabric SDK
Open PowerShell in administrator mode and run the following command to enable Visual Studio to deploy to the local Service Fabric
cluster:powershell Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force -Scope CurrentUser
Creating Microservice Using Azure Service Fabric
Launch Visual Studio as an administrator, create a new project with File->New->Project and Select Service Fabric Application from Cloud category. Name the application as ServiceFabricDemo and click OK.
On the New Service Fabric Service choose Stateless ASP.NET Core template and name your service as per your choice. Here I am going with the default name as Web1, and then click OK.
On the next window choose API as the template and then click Ok and it will create your Service Fabric Application
Here our solution that has 2 projects. The first project is the application project this has no code in it. Which ends up creating the application manifest XML file. If I open this node you'll see there's a services entry in the tree and if I open the services entry, you'll see one entry here for Web1. You can right click on services here and you can add additional service fabric services into the collection. The next we are having our real service where our codes lie. If you look at the properties for this project you can see it is just a .NET console application.
Understanding Azure Service Fabric Local Cluster
The Azure Service Fabric Local Cluster get shipped with the Service Fabric SDK and you can get it installed on your local machine. You can run your local cluster from the taskbar. Right-clicking the icon will open the context menu with the following options.
Exit : Removes the icon off the tray. It won’t kill the actual local cluster, it just kills that utility.
Remove Local Cluster : Removes all the files for the local cluster from your hard disk and clean up the hard disk space.
Switch Cluster Mode : By default, the services of the local cluster will be running on one node. Using this option, you can make it running on 5 nodes.
Stop Local Cluster : Stops the local cluster from running, so it's not wasting any CPU cycles but it is leaving all the files on your desk so you could start the local cluster again whenever you want.
Start Local Cluster : The option is disabled out here as it is already running. You can start, if you have stopped it.
Reset Local Cluster : You can use this option to bring down the local cluster, get everything out of it, resetting it and then start it back up again.
Manage Local Cluster : This will launch the local cluster on your default browser and you can see what’s happening inside it.
Selecting Manage Local Cluster of double-clicking the icon will you to the dashboard of the local cluster in your default browser. Here it will show the number of Applications and nodes. As we haven’t deployed it won’t show any applications here
In the Address Bar, you can see that it is starting off with localhost as your cluster is running on your local machine. In case if you have a cluster running on Amazon data center or an Azure data center, the localhost will be replaced with the IP address of that location. It is also possible to secure these with certificates making only those client machines having a certificate can communicate with the server. Service Fabric uses port 19080 as an endpoint for making REST request to Service Fabric components within a cluster and the rest of this part of the address is a like a subsection right you can drill into various parts of the cluster.
The Service Fabric Explorer interface has a refresh rate where it will periodically pull the cluster to get new information. You can adjust as per your choice. Setting it to fast which has it refreshing every two seconds where you can see what's happening in the cluster as quickly as possible. In the left-hand plane, you can see that the cluster has broken down into three sections: Applications, Nodes, and System. Applications is a kind of software view on what is happening within the cluster while nodes are more like a hardware view which shows you the nodes that are in the cluster.
By default, in the local cluster, you will be having only one node which you can change from the context menu. For each node, a separate instance of fabric.exe and fabricateway.exe will be running on your local machine. As this is a brand-new cluster that has nothing going on presently. The System section will show the system services for the cluster. Basically, there will be six and only four out of six are available on local cluster and the other two ImageStoreService and UpgradeService only exist in the Azure data center.
Run your .NET Core API Application from Visual Studio and you can see the application has updated here.
Testing Microservice Using Postman
Running the application will also launch the application in the browser with URL as below which comes from the ValuesController.cs controller class.
Let’s update the second method to return a specific value in our controller class to return a value we pass instead of the string “value”. Update the code as below
Here we can update the other action methods also, which are harder to test in the browser as browser typically takes GET request and they can only send post request if we set up a form. Instead we can use a simple tool called Postman for sending HTTP requests to your APIs and you can see how those actions actually works. Its s free community tool and you can get it form
https://www.getpostman.com/. Download and install Postman and add the URL appended with the test value and submit GET request and you can get the result as expected.
In the same method you can test for other action methods also by putting breakpoints or writing the action methods. For that you may need to change the body form-data to raw JSON data.
So, in this tutorial, we build a Microservice with ASP.NET Core using Azure Service Fabric and tested it using Postman. Mastering Microservices training program enabled you to learn. How to implement Microservices architecture in your existing projects and upcoming projects with confidence.
Take our free skill tests to evaluate your skill!
In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.