Search code examples
c#azureazure-service-fabricservice-fabric-stateful

Access Azure Service Fabric stateful service state


I have added WebAPI to my stateful serive and want to access StateManager from it (from outside of StatefulService class implementation).

What is the best way to do it?

Currently I am using a small class for this:

internal static class InstanceAccessor
{
   internal static StatefulService ActiveInstance { get; set; }
}

And put the value inside RunAsync method of the StatefulService :

protected override async Task RunAsync(CancellationToken cancellationToken)
{
    InstanceAccessor.ActiveInstance = this;
    ...

Solution

  • There's a sample project on Github that shows a simple way to do it using dependency injection.