Getting Started with Azure Service Fabric
Published: 2020-03-23 05:50:34

---
title: Getting Started with Azure Service Fabric
published: true
date: 2020-03-23 00:00:00 UTC
tags: 
canonical_url: https://sahansera.dev/getting-started-with-azure-service-fabric/
---

> tl;dr - if you only want to get started quickly, just jump ahead to “Setting up a Development Environment” section

I have been experimenting with container orchestrators to help me deploy a microservices-style hobby project I have been working on (more on this in a separate blog post) to the cloud. If there’s one thing I learnt from this project, there are a lot of moving parts to it and might not be easy to manage a distributed cluster with hundreds of services that are fast, reliable and scaling.

While I was on the journey in finding a platform/framework with a good balance of container orchestration, managing and monitoring infrastructure, I came across Azure Service Fabric which looked promising.

The following definition is the one that caught my eye,

> The aim of Service Fabric is to solve the hard problems of building and running a service and to use infrastructure resources efficiently, so teams can solve business problems by using a microservices approach. Service Fabric is agnostic about how you build your service, and you can use any technology. But it does provide built-in programming APIs that make it easier to build microservices.

For starters, you can have a quick look at [Microsoft’s official page](https://docs.microsoft.com/en-au/azure/service-fabric/service-fabric-overview) to learn more about Azure Service Fabric.

![./1-service-fabric-intro.png](https://sahansera.dev/static/14ab6211e40e118b64600d6c5e73557a/11d19/1-service-fabric-intro.png)

Source: [https://docs.microsoft.com/en-au/azure/service-fabric/service-fabric-overview](https://docs.microsoft.com/en-au/azure/service-fabric/service-fabric-overview)

### Options in Azure for Microservices

Here’s a quick summary of different options you can use to build microservices type applications in Azure,

- **AKS**: Microsoft’s Managed Kubernetes service with Docker as runtime
- **Service Fabric**: Another step forward from AKS. All about microservices and,- Service communication- Service discovery- Monitoring and Telemetry- Provision and upgrade- Testing locally- Scalability- Manage downtimes

  - Focus on business objectives and infrastructure. In AKS you need to build your infrastructure. Build and deploy from day1, but still be able to customize.
  - Proven tech - Many azure services run on ASF. Cosmos DB, Azure SQL, Power BI, Skype, IoT Hub, Cortana, ACR, Azure DevOps
- **Azure Functions** - Mini version of microservices. Event-driven (triggers, scheduled). Easier to get started than Azure Service Fabric and Azure Kubernetes Service

### Supported Environments

- **Public Clouds**

Service Fabric is designed to run on any cloud platform other than Azure, such as AWS, GCP etc. However, as you might have already guessed, Azure has first-class support.

- **On-Premise**

If you want to manage your own premise cluster, ASF supports Windows Server 2012 R2, 2016 or higher, Linux Ubuntu 16.04 and 18.04. However, when it comes to Macs, you are kind of on your own since you will have to rely on an emulator for that.

- **Developer Workstation**

Service Fabric does not have a dependency on Visual Studio since it’s designed to be modular. This is great because you can have a similar setup to your production environment in your development machine. It also brings you much good stuff such as simulating real-life disasters and fixing issues before they go into production!

### Programming Models

Azure service fabric provides us with different ways to wrap our services in. Here’s a summary of programming models used in Service Fabric.

- [Reliable Service](https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-services-quick-start): Used to address single or multiple business concerns.
  - Stateless - Acts like a console application
  - Stateful - With its own transactional replicated storage.
- [Reliable Actors](https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reliable-actors-introduction): Service Fabric uses virtual actors to handle communication between servers. Here’s an [excellent article on Actor Model](https://mattferderer.com/what-is-the-actor-model-and-when-should-you-use-it)
- [Guest Executables](https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-guest-executables-introduction): run any executable without any code changes
- [Containers](https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-containers-overview): Separate blog post will be published on this (I promise!)

### Setting up a Development Environment

To set up your _local_ environment, we need to first install the necessary tooling. You can also have a look at Service Fabric’s [official documentation](https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-get-started) on getting started.

Make sure you follow these steps,

1. Install Visual Studio 2019 - Community edition is fine. In fact, you can use an editor such as Visual Studio Code. But it would be easier to use VS to get something up and running.
2. Install Service Fabric tooling

![./2-installing-tools.png](https://sahansera.dev/static/788ccd5f8c7520349977417626da1be3/11d19/2-installing-tools.png)

3. Install Service Fabric SDK - If you run Visual Studio and try to create a new Service Fabric project it will ask you whether you want to install the Service Fabric. However, you can also install the SDK with the [Microsoft Web Platform Installer](https://www.microsoft.com/web/downloads/platform.aspx) (don’t forget to scroll down to _Download Web PI_ section)

![./3-installing-sdk.png](https://sahansera.dev/static/163e9e86a1414f7d91029db8177044f9/11d19/3-installing-sdk.png)

### Verifying your local setup

Once you have installed the prerequisites, you can open up Visual Studio and select **_Create a new project_**

![./4-new-project.png](https://sahansera.dev/static/278d780b5e2f79684e4308e249eb10d4/11d19/4-new-project.png)

In the **_Create a new project_** dialog, filter by typing “fabric” and select **_Service Fabric Application_**

![./5-new-project-2.png](https://sahansera.dev/static/0580c0ec92d28357ada2b784eda6d6e0/11d19/5-new-project-2.png)

Enter a project name. You don’t need to worry about Framework version unless you are working on a classic ASP application.

![./6-new-project-3.png](https://sahansera.dev/static/5a7284bb4101f0153bbf2f03ef75ce23/11d19/6-new-project-3.png)

You will get another popup to select a project type. For this demo, we will select **_Stateless Service_** and enter a name for our project.

![./7-new-project-4.png](https://sahansera.dev/static/69747af08d2b8a3c54c32e3e0e33c377/11d19/7-new-project-4.png)

In your solution explorer you will find 2 projects like so, you can also double-check with my [repo](https://github.com/sahan91/service-fabric-stateless.git) if you managed to get this far.

![./8-solution-explorer.png](https://sahansera.dev/static/3cecf4b6e5d94001a7b1a4b84aecaabc/71275/8-solution-explorer.png)

The first project is just a wrapper around the Verification Project. If you open up `Verification.cs` you will see that in `RunAsync` method is the main entry point of our stateless console app. Hit F5 and see whether your local cluster is working fine. It may take about 2 - 3 mins initially.

Once everything is up and running, you will see the following output in Visual Studio. Put a breakpoint in your code and have a play around it.

![./9-solution-diagnostics.png](https://sahansera.dev/static/afd72e1ffb2a56b86f7912604d95ec7d/11d19/9-solution-diagnostics.png)

Now open up a browser and navigate to `http://localhost:19080/`

You will be able to see that your application is running in a healthy state in your local cluster.

![./10-sf-explorer.png](https://sahansera.dev/static/1e3b4d123a8da6ababf672206565dfc9/11d19/10-sf-explorer.png)

Congratulations for making it this far! Stay tuned for my next post on **_deploying a multi-container application to Azure with Service Fabric_**

### References

1. [https://docs.microsoft.com/en-au/azure/service-fabric/service-fabric-overview](https://docs.microsoft.com/en-au/azure/service-fabric/service-fabric-overview)
2. [https://docs.microsoft.com/en-au/azure/service-fabric/service-fabric-architecture](https://docs.microsoft.com/en-au/azure/service-fabric/service-fabric-architecture)
3. [https://docs.microsoft.com/en-us/azure/architecture/reference-architectures/microservices/service-fabric](https://docs.microsoft.com/en-us/azure/architecture/reference-architectures/microservices/service-fabric)