Create your first Azure Service Fabric application

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).

Prerequisites

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.

You will also want to configure the Azure CLI 2.0 (recommended) or XPlat CLI for deploying your application.

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.

  1. In a terminal, type the following command to start building the scaffolding: yo azuresfcsharp
  2. Name your application.
  3. Choose the type of your first service and name it. For the purposes of this tutorial, we choose a Reliable Actor Service.

    Service Fabric Yeoman generator for C#

Note

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

  1. Connect to the local Service Fabric cluster.

    azure servicefabric cluster connect
    
  2. 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.

    ./install.sh
    

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.

  1. Run the script using the watch utility to see the output of the actor service.

    cd myactorsvcTestClient
    watch -n 1 ./testclient.sh
    
  2. In Service Fabric Explorer, locate node hosting the primary replica for the actor service. In the screenshot below, it is node 3.

    Finding the primary replica in Service Fabric Explorer

  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:

  1. Change directory to the root of the existing application. For example, cd ~/YeomanSamples/MyApplication, if MyApplication is the application created by Yeoman.
  2. Run yo azuresfcsharp:AddService

Migrating from project.json to .csproj

  1. Running 'dotnet migrate' in project root directory will migrate all the project.json to csproj format.
  2. Update the project references accordingly to csproj files in project files.
  3. Update the project file names to csproj files in build.sh.

Next steps