In this article, we will create Stateless
Service Fabric Application and understand different component of project.
Step 1: Select Service Fabric Application
from Cloud Template. Name the project of
your choice and click OK.
Step 2: Now select Stateless Service
template and provide service name. Click Ok to continue.
Step 3: Let’s open the solution explorer
and expand application project and then Services. You will notice reference of
MyStatelessService. You can add multiple Service Fabric application.
Step 4: Next we have
ApplicationManifest.xaml in ApplicationPackageRoot which contains service type
name, instant count and other things.
Step 5: ApplicationParameters folder
contain Cloud.xml, Local.1Node.xml and Local.5Node.xml. Cloud.xml used while deploying in cloud, it
contains instant count with value as -1. -1 means deploy every instance of
service in every node of cluster.
Local.1Node.xml and Local.5Node.xml contains
instant count value as 1 because it will be deployed in one node of cluster.
Step 6: Script folder contains
Deploy-FabricApplication.ps1 PowerShell script which deploys the application in
cloud and lot many other things.
Step 7: And last thing in application
project is packages.config which configuration around builds.
Step 8: Right click on service and click on
properties, you will notice that Service is normal Console Application project
like any other .NET console application.
Step 9: Open references and you will notice
Service Fabric reference added which enables the console application to access
the Classes, Enumerations, Interfaces, Methods and many more things.
Step 10: Open PacakgeRoot, it contains
ServiceManifest.xml. This file contains Service Type Name, Name of Exe and
Service End Points.
Step 11: Open Program.cs which contains
Main method like any other console application.
ServiceRuntime.RegisterServiceAsync is static method which tells service fabric
to register the service.
The first parameter is Service Type Name
defined in the Manifest file, second parameter is the instance of
MyStatelessService class which gives the context of the service.
RegisterServiceAsync is async method but we
can’t put await in main so GetAwaiter().GetResult() is used.
Thread.Sleep blocks the main thread
infinitely to ensure process doesn’t terminate so that Service fabric can
create replicas and instances.
Step 12: CreateServiceInstanceListeners
method listen to endpoints like HTTP, TCP and UDP. By default I listens to only
listener which is HTTP.
RunAsync method is called when instance is
running. This will run over the life time of the service and the code which
needs to be run always goes over here like getting message from queue.
RunAsync method takes CancellationToken
parameter. It is very important to use it; it will help in graceful upgrade of
service of bringing down service gracefully. CancellationToken is used to
signal the service to clean up and go down gracefully.
This ends the article of creating stateless
service and understanding the project.
|