How to Convert a Stateless Azure Service Fabric Service to a Stateful Service

I built a stateless Azure Service Fabric service and later realized I would need a stateful service. Rather than try to comb through docs to see what the conversion looked like, I created a brand new stateless service and another stateful service and diff’d them. I’m sharing in case someone else needs to do the same.

You can see everything you need to do in this commit: https://github.com/jongio/azure-service-fabric-stateless-to-stateful-conversion/commit/168c42776fbd4d23f5f8d0ebc943a9ee6fe2129f

Or you can follow along below…

MySolution/MyService/MyService.cs

Add Microsoft.ServiceFabric.Data.Collections namespace

Derive from StatefulService instead of Stateless service and inject StatefulServiceContext

Use ServiceReplicaListener instead of ServiceInstanceListener

Use ReliableDictionary/ReliableQueue as needed by your service logic.

MySolution/MyService/PackageRoot/Config/Settings.xml

Add ReplicatorConfig section to config

MySolution/MyService/PackageRoot/ServiceManifest.xml

Set “HasPersistedState” variable in ServiceManifest

Change StatelessServiceType to StatefulServiceType

MySolution/MyService/ServiceEventSource.cs

Inject StatefulService and set ReplicaId instead of InstanceId

MySolution/MyServiceFabricApplication/ApplicationPackageRoot/ApplicationManifest.xml

Add Replica settings to ApplicationManifest

Change StatelessService element to StatefulService Element

MySolution/MyServiceFabricApplication/ApplicationParameters/Cloud.xml

Add Parameters to your Cloud and Local.xml files

MySolution/MyServiceFabricApplication/ApplicationParameters/Local.xml

Jon