In this article, we will create our first
Service Fabric application like Hello World.
Step 1: Install
the Microsoft Azure Service Fabric SDK and Tools
Step 2: Right click on Visual Studio and
click on “Run as Administrator”. Running as administrator is required to debug
in local cluster.
Step 3: Go to File->New
Project->Cloud->Service Fabric Application. You won’t see Service Fabric
Application template if you haven’t installed the Azure Service Fabric SDK from
the link provided in Step 1.
Step 4: Click on Ok and Select Stateful
Service. Enter Name of Stateful Service and click OK again.
Step 5: Now once you click OK, your first
Azure Service Application is almost ready. Open MyStateFulService.cs and you
will notice two methods there CreateServiceReplicaListeners
and RunAsync. At this moment, we will CreateServiceReplicaListeners, RunAsyn is
the main entry point of service replica. The RunAsync has code which increments
the counter, we won’t go in much detail of the code as we are getting started.
Step 6: Open
solution explorer and you will notice two projects MySeriveApplication and
MyStatefulServce. MyApplicationService
doesn’t contain any code, it contains configuration and reference of
MyStatefulSerivce. MyStatefulService
contains the code and logic which executes.
Step 7: Now run
the application, it will take few minutes to start the Service Fabric Local
Cluster Manager. Once it is started, your application will be deployed in the
nodes of cluster and ready for execution. You will see below Diagnostic Events
screen.
Step 8: When
you expand ServiceMessage with counters, you will notice the code is running in
_Node_3. This might be different for you.
Step 9: Now
open System tray and you will see Service Fabric Local Cluster Manager. This is
not simulation of Service Fabric Environment; it is real service fabric cluster
running with 5 nodes.
Now right click
and choose Manage Local Cluster option.
Step 10: Service
Fabric Explorer show two views and Applications and Nodes. You will notice that
there are two applications and 5 nodes.
Expand Application which will bring
MyServiceApplicaitonType and MyStatefulApplicationType.
Step 11: Expand
MyServiceApplicationType and you will see deployed application and _Node_3 as
primary replica where service is running.
Step 12: Put a breakpoint
in the first line of RunAsync method of MyStateFulService.cs. Now go back to
Service Fabric Explorer and select the primary node in which your service is
running. From right top corner of the screen select Actions dropdown and Select
Deactivate (restart). This will move the service to another node in the
cluster.
Step 13:
Confirm node name by entering the node name you want to Restart and click
Restart.
Step 14: You
will notice the breakpoint will be hit because when Node change happens service
starts executing again.
Open DiagnostivEvents and expand one of the Service
Message, you will notice now the service is running on _Node_1.
If you notice
even though Node switching happened the state of service is not lost even
though there is momentary delay in node switching
This ends the
article of getting started with Service Fabric.
|