# Dapr on Service Fabric Sample

## Sample info

| Attribute | Details |
|--------|--------|
| Dapr runtime version | v1.4.0 |
| Language | Javascript |
| Environment | Service Fabric |

## Overview

The following is a sample of how to host a Dapr app on a Service Fabric cluster. We'll be deploying a Node.js app that subscribes to order messages and persists them. The following architecture diagram illustrates the components that make up this sample:

![Architecture Diagram](./img/Architecture_Diagram.png)

Some Dapr functionality, including Pub/Sub, Secrets and Bindings work without any integration with SF. However, service invocation, will not work without additional changes and is out of scope for this post.

## Prerequisites

Clone this repo using `git clone https://github.com/dapr/samples.git` and go to the directory named *\hello-service-fabric*. All commands should be executed from a PowerShell terminal.

- [Docker](https://docs.docker.com/)
- [PowerShell](https://github.com/powershell/powershell)
- [REST Client extension](https://marketplace.visualstudio.com/items?itemName=humao.rest-client)
- [Node.js version 8 or greater](https://nodejs.org/en/)
- [Microsoft Azure Service Fabric SDK](https://www.microsoft.com/web/handlers/webpi.ashx?command=getinstallerredirect&appid=MicrosoftAzure-ServiceFabric-CoreSDK)

## Step 1 - Setup Dapr

Follow these [instructions](https://docs.dapr.io/getting-started/install-dapr/#install-dapr-in-self-hosted-mode) to download and install the Dapr CLI and initialize Dapr with `dapr init`.

```PowerShell
dapr init
```

## Step 2 - Setup Service Fabric

1. Create a basic Service Fabric cluster through the SDK as described [here](https://docs.microsoft.com/azure/service-fabric/service-fabric-get-started).

1. Right click "Service Fabric Local Cluster Manager" in the taskbar and select "Start Local Cluster".

## Step 3 - Create the Service Fabric Application Package

This section will create an application package where daprd.exe and the user node.js app will be run as guest executables in two separate code packages under the same Service Fabric service. They'll always be co-located.

1. Copy daprd.exe from your local Dapr installation, `c:\Users\[user]\.dapr\bin`, into `\daprsfpkg\MyService\CodeDapr\`.

   ```PowerShell
   copy c:\Users\$env:username\.dapr\bin\daprd.exe .\daprsfpkg\MyService\CodeDapr\
   ```

1. Copy the components from your local Dapr installation, `c:\Users\[user]\.dapr\components`, into `\daprsfpkg\MyService\CodeDapr\components`.

   ```PowerShell
   copy c:\Users\$env:username\.dapr\components\*.* .\daprsfpkg\MyService\CodeDapr\components\
   ```

1. From `\daprsfpkg\MyService\CodeUserApp\`, run `npm install` to install the node.js app's dependencies and return to `\hello-service-fabric`.

   ```PowerShell
   cd .\daprsfpkg\MyService\CodeUserApp\
   npm install
   cd ..\..\..
   ```

1. From `\hello-service-fabric`, confirm the application package is well-formed:

   ```PowerShell
   Test-ServiceFabricApplicationPackage .\daprsfpkg\
   ```

Notes:

- the application package includes the Dapr `components` folder, which stores Dapr component configurations, in the Dapr code package.  In Dapr standalone (local) mode, this is generated by the Dapr CLI. It is included so daprd.exe can read it when running on a Service Fabric cluster.

- the service manifest contains a command to invoke daprd.exe with the same args that the Dapr CLI does in the sample.

## Step 4 - Create the Service Fabric Application

Copy the application package to the cluster, register it, then create the application.

Connect to the cluster:

```PowerShell
Connect-ServiceFabricCluster
```

Copy the application package to the cluster:

```PowerShell
Copy-ServiceFabricApplicationPackage daprsfpkg
```

Register it:

```PowerShell
Register-ServiceFabricApplicationType daprsfpkg
```

Create the application instance:

```PowerShell
New-ServiceFabricApplication -ApplicationName fabric:/dapr1 -ApplicationTypeName daprsf -ApplicationTypeVersion 1.0
```

daprd.exe and the node.js app will run on the same node.

## Part 5 - Post Messages to the Service

Now that Dapr and the Node.js app are running, you can publish events to the pubsub component, using the Visual Studio Code [REST Client extension](https://marketplace.visualstudio.com/items?itemName=humao.rest-client).

Open the `requests.http` file in the root folder to execute the following requests.

```text
POST http://localhost:3500/v1.0/publish/pubsub/neworder
Content-Type: application/json

{"order":{"orderId":"44"}}

###

GET http://localhost:3500/v1.0/invoke/mynode/method/order
```

## Part 6 - Cleanup

1. Delete the Service Fabric application instance:

   ```PowerShell
   Remove-ServiceFabricApplication -ApplicationName fabric:/dapr1 -Force
   ```

1. Unregister the Service Fabric application type

   ```PowerShell
   Unregister-ServiceFabricApplicationType -ApplicationTypeName daprsf -ApplicationTypeVersion 1.0 -Force
   ```