Service Fabric provides SDKs for building services on Linux in both .NET Core and Java. In this tutorial, we look at how to create an application for Linux and build a service using C# (.NET Core).
Before you get started, make sure that you have set up your Linux development environment. If you are using Mac OS X, you can set up a Linux one-box environment in a virtual machine using Vagrant.
Create the application
A Service Fabric application can contain one or more services, each with a specific role in delivering the application's functionality. The Service Fabric SDK for Linux includes a Yeoman generator that makes it easy to create your first service and to add more later. Let's use Yeoman to create an application with a single service.
- In a terminal, type the following command to start building the scaffolding:
- Name your application.
Choose the type of your first service and name it. For the purposes of this tutorial, we choose a Reliable Actor Service.
For more information about the options, see Service Fabric programming model overview.
Build the application
The Service Fabric Yeoman templates include a build script that you can use to build the app from the terminal (after navigating to the application folder).
cd myapp ./build.sh
Deploy the application
Once the application is built, you can deploy it to the local cluster.
Using XPlat CLI
Connect to the local Service Fabric cluster.
azure servicefabric cluster connect
Run the install script provided in the template to copy the application package to the cluster's image store, register the application type, and create an instance of the application.
Using Azure CLI 2.0
Deploying the built application is the same as any other Service Fabric application. See the documentation on managing a Service Fabric application with the Azure CLI for detailed instructions.
Parameters to these commands can be found in the generated manifests inside the application package.
Once the application has been deployed, open a browser and navigate to Service Fabric Explorer at http://localhost:19080/Explorer. Then, expand the Applications node and note that there is now an entry for your application type and another for the first instance of that type.
Start the test client and perform a failover
Actor projects do not do anything on their own. They require another service or client to send them messages. The actor template includes a simple test script that you can use to interact with the actor service.
Run the script using the watch utility to see the output of the actor service.
cd myactorsvcTestClient watch -n 1 ./testclient.sh
In Service Fabric Explorer, locate node hosting the primary replica for the actor service. In the screenshot below, it is node 3.
- Click the node you found in the previous step, then select Deactivate (restart) from the Actions menu. This action restarts one node in your local cluster forcing a failover to a secondary replica running on another node. As you perform this action, pay attention to the output from the test client and note that the counter continues to increment despite the failover.
Adding more services to an existing application
To add another service to an application already created using
yo, perform the following steps:
- Change directory to the root of the existing application. For example,
cd ~/YeomanSamples/MyApplication, if
MyApplicationis the application created by Yeoman.
Migrating from project.json to .csproj
- Running 'dotnet migrate' in project root directory will migrate all the project.json to csproj format.
- Update the project references accordingly to csproj files in project files.
- Update the project file names to csproj files in build.sh.
- Learn more about Reliable Actors
- Interacting with Service Fabric clusters using the Azure CLI
- Learn about Service Fabric support options