vmn vmn - 3 years ago 188
ASP.NET (C#) Question

Azure Service Fabric: Using Reliable collections in ASP.NET Core Stateful Service directly

I built a Service Fabric Application with two services:

  1. Stateful Data service that stores data in a reliable dictionary and exposes methods to add/remove/get data items from dictionary.

  2. A stateless Web API service that acts a HTTP interface to outside world and that communicates using remoting to Data service to get data and display to user.

There is a provision to create stateful Web API service in Service Fabric. I tried to get rid of the data service and manage the reliable dictionary in the Web API service only. However, I am not able to access the StateManager in my controller. I am not able to find any samples online that do this.

Is my understanding correct about the stateful ASP.NET Core API services? That is, I can use reliable collections directly in it?

Answer Source

Yes - you can use reliable collections in an asp.net service. This sample is equal to what you are building: https://github.com/Azure-Samples/service-fabric-dotnet-quickstart it contains an asp.net core stateful service.

Main things:

  1. Your service has to derive from Microsoft.ServiceFabric.Services.Runtime.StatefulService
  2. In the serviceListener, you setup a ServiceReplicaListener (not ServiceInstanceListener)
  3. In WebHostbuilder you add a singleton IRealibleStatemanager: .AddSingleton(this.StateManager))

Now your controllers will have access to the singleton StateManager:

 public MyDataController(IReliableStateManager stateManager)
       this.stateManager = stateManager;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download