Hands on with Service Fabric – Reliable Services

By now, we have set a strong foundation for us to explore the internals of Service Fabric. Let's get started by building our first Service Fabric application. We will build a simple application that will print the customary Hello World message.

The companion GitHub repository of this title contains code for all the samples that we have used in this book. You can visit https://github.com/PacktPublishing/Microservices-with-Azure to download the samples. Let's start building our application:

  1. Launch Visual Studio as an administrator. This is necessary because we are going to test our application in Service Fabric local cluster that needs administrator privileges to work.
  2. Click File| New Project | Cloud | Service Fabric Application.

 

 

  1. Name the application HelloWorldApplication and click OK.
Create new Service Fabric application
  1. On the next page, choose Stateless Service as the service type to include in your application. Name it HelloWorldService and click OK.
Create new stateless service dialog

Wait for the project template to unfold. In your solution, you will find two projects – the Service Fabric application project, named HelloWorldApplication and the stateless Microservice named HelloWorldService. Remember that in Service Fabric an application is a collection of Microservices. We will go through the various components of the project in a little while. For now, let us make our HelloWorldService generate some output. Navigate to the HelloWorldService.cs file and locate the HelloWorldService class.

      namespace HelloWorldService 
{
/// <summary>
/// An instance of this class is created for each service
instance by the Service Fabric runtime.
/// </summary>
internal sealed class HelloWorldService : StatelessService
{
public HelloWorldService(StatelessServiceContext
context)
: base(context)
{ }

/// <summary>
/// Optional override to create listeners (for example,
TCP, HTTP) for this service replica to handle client
or user requests.
/// </summary>
/// <returns>A collection of listeners.</returns>
protected override IEnumerable<ServiceInstanceListener>
CreateServiceInstanceListeners()
{
return new ServiceInstanceListener[0];
}

/// <summary>
/// This is the main entry point for your
service instance.
/// </summary>
/// <param name="cancellationToken">Canceled when
Service
Fabric needs to shut down this service
instance.</param>
protected override async Task RunAsync(CancellationToken
cancellationToken)
{
...
}
}
}

A Service Fabric stateless Microservice derives from the StatelessService class. Service Fabric internally uses the functions exposed by the StatelessService class to govern the lifecycle of your stateless Microservice. The RunAsync method is a general-purpose entry point for your service code.

Let's replace the definition of this method with the following one:

        protected override async Task RunAsync(CancellationToken 
cancellationToken)
{
long iterations = 0;
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
ServiceEventSource.Current.ServiceMessage(this.Context,
$"Hello World {++iterations}");
await Task.Delay(TimeSpan.FromSeconds(1),
cancellationToken);
}
}

Service Fabric does not force your Microservices to communicate through any specific communication stack. Since we haven't implemented any communication stack for our service in this example, our service will behave as a background service akin to Windows Service, Azure WebJob, or Cloud Service Worker Role.

The Service Fabric solution template also adds an Event Source implementation to create events for Event Tracing for Windows (ETW) for you. ETW is an efficient kernel-level tracing facility that lets you log kernel or application-defined events. Service Fabric runtime itself logs events using ETW and using the same logging mechanism would help you understand how your service communicates with the Service Fabric runtime. ETW can work across cloud environments and even on your local system. Therefore, using ETW can make your application platform independent.

  1. Run the application by clicking on Run or by pressing F5. At this time, visual studio will spin up a local cluster for development. Once your application gets deployed on the cluster, you would be able to see the ETW trace events in Diagnostic Events Viewer window of Visual Studio:
Output of  Hello World stateless service
  1. You can expand any event to see more details. For instance, in the preceding event, you can view the node and partition on which your Microservice is running.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset