1
 [Route("testApp/{Id}")]
    [HttpGet]
    public async Task<List<string>> testApp(Guid Id)
    {
        return await new Heavy().WorkOnDictionary(Id);

    }

I have created a stateful service and has implemented REST api calls in one of the call I want to access the IReliableDictionary but i cannot do that it asks for stateManager I want to access IReliableDictionary inside WorkOnDictionary() function which is defined inside a class Heavy

public async Task<List<string>> WorkOnDictionary(Guid Id){
IReliableDictionary<string, List<string>> Dictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, List<string>>>("stringlistcache");                                                       
   string monitorName=convertIdtoMonitor(Id);
using (var tx = this.StateManager.CreateTransaction())
{
  var cachedValue = await Dictionary.TryGetValueAsync(tx, monitorName);
  await tx.CommitAsync();
  if(cachedValue.HasValue) 
    return cachedValue.Value
    else 
    {
        var res=await GetdetailFrmRemote(monitorName)
            Dictionary.AddAsync(tx,monitorName,res);
                return res;
    }}
}

How should I initialize StateManager?

Baadshah shah
  • 77
  • 1
  • 11
  • Please share some code. Hard to tell what the problem is otherwise. – Peter Bons Jun 27 '17 at 11:07
  • @PeterBons Added code – Baadshah shah Jun 27 '17 at 11:20
  • I assume `Heavy` is the stateful service and your web api is implemented in a stateless service? You are not supposed to create an instance of a ASF service this way. You should communicate with it using for example remoting. See https://stackoverflow.com/questions/36589435/accessing-a-stateful-service – Peter Bons Jun 27 '17 at 11:35
  • Otherwise, If you added a web api to your stateful services, take a look at this: https://stackoverflow.com/questions/42049901/access-azure-service-fabric-stateful-service-state – Peter Bons Jun 27 '17 at 11:36
  • @PeterBons Heavy is not statefull class if i make it statefull then i have to pass context in its constructor and i don't know where to get context from.. – Baadshah shah Jun 27 '17 at 11:59
  • That is not how it works. You should never create an instance of a stateful service to access it methods. Instances are created by Service Fabric itself (see the program.cs file in your service project). You should always communicate with it using for example remoting. You have to get a clear understanding about the concepts of service fabric inter-service communication before attempting to create an application with it. But this is too broad for this question. Please read the links I provided carefully. – Peter Bons Jun 27 '17 at 12:46

1 Answers1

0

If I'm reading this correctly, it looks like you want to use IReliableStateManager in an MVC controller. In that case, simply inject it into your controller. ASP.NET Core has a nice DI system built in. The Service Fabric getting-started sample code shows you how to do this: https://github.com/Azure-Samples/service-fabric-dotnet-getting-started/tree/master/src/GettingStartedApplication/StatefulBackendService

Vaclav Turecek
  • 9,020
  • 24
  • 29