Hello, Service Fabric!

  • 8/31/2016

Hello, World

This is the moment you’ve been waiting for—a chance to implement the beloved “Hello, World” in a new way.

Now you are ready to create your first Service Fabric application. This application contains a stateless service that generates a time stamped “Hello World” string every five seconds.

  1. Launch Visual Studio 2015 as an administrator.

  2. Create a new project named HelloWorldApplication using the Cloud\Service Fabric Application template, as shown in Figure 1-9.

    FIGURE 1-9

    FIGURE 1-9 New Project dialog box

  3. In the New Service Fabric Service dialog box, select the Stateless Service template, enter HelloWorldService as the Service Project Name, and then click OK to create the Hello World service, as shown in Figure 1-10.

    FIGURE 1-10

    FIGURE 1-10 New Service Fabric Service dialog box

  4. Now, in the solution you have two projects: A HelloWorldApplication project for the Service Fabric application and a HelloWorldService project for the stateless service. You’ll go through the rest of the solutions in a moment. For now, focus on the HelloWorldService class in the service project:

    internal sealed class HelloWorldService : StatelessService
    {
        public HelloWorldService(StatelessServiceContext context)
            : base(context)
        { }
        protected override IEnumerable<ServiceInstanceListener>
    CreateServiceInstanceListeners()
        {
            return new ServiceInstanceListener[0];
        }
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            long iterations = 0;
            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();
                ServiceEventSource.Current.ServiceMessage(this, "Working-{0}", ++iterations);
                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
            }
        }
    }
  5. To implement a stateless service, your service class needs to inherit from the StatelessService base class. Service Fabric doesn’t mandate a communication protocol. Instead, you can plug in different communication stacks by providing an ICommunicationListener implementation. You’ll see a number of communication stack implementations throughout this book. For this example, you’ll skip the communication stack, which means your service is a background service that doesn’t take any client requests.

  6. Modify the RunAsync method to implement your service logic.

    protected override async Task RunAsync(CancellationToken cancellationToken)
    {
      while (!cancellationToken.IsCancellationRequested)
      {
    ServiceEventSource.Current.ServiceMessage(this, "Hello World at " +
    DateTime.Now.ToLongTimeString());
        await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
      }
    }

    As you can see in this code snippet, to implement a background service, all you need to do is override the RunAsync method and construct your processing loop.

  7. Service Fabric SDK automatically generates an Event Tracing for Windows (ETW) event source implementation for you to generate trace events. Examine the generated ServiceEventSource class if you are interested.

  8. Now, you can press F5 to run the application. Visual Studio will launch a test cluster, deploy the application, and start the services. Once the service is launched, you can see the “Hello World” output in the Diagnostic Events window, as shown in Figure 1-11.

    FIGURE 1-11

    FIGURE 1-11 Diagnostic Events window

  9. Click Stop Debugging on the Visual Studio toolbar or press Shift+F5 to stop the debugging session. If you bring back the Diagnostic Event view, you can see the “Hello World” strings are still coming in. This is because the Stop Debugging button only stops your debugging session; it doesn’t stop the service.

    Congratulations! You’ve implemented, deployed, and tested your first Service Fabric application. Next, you’ll take a peek into the local cluster.