如何在服务结构中访问IReliableDictionary

时间:2017-06-27 10:17:47

标签: azure-service-fabric stateful

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

    }

我已经创建了一个有状态服务,并且在我要访问IReliableDictionary的一个调用中实现了REST api调用但是我不能这样做它要求stateManager 我想在WorkOnDictionary()函数中访问IReliableDictionary,该函数在类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;
    }}
}

我应该如何初始化StateManager?

1 个答案:

答案 0 :(得分:0)

如果我正确读取它,看起来你想在MVC控制器中使用IReliableStateManager。在这种情况下,只需将其注入您的控制器。 ASP.NET Core内置了一个很好的DI系统.Service Fabric入门示例代码向您展示了如何执行此操作:https://github.com/Azure-Samples/service-fabric-dotnet-getting-started/tree/master/src/GettingStartedApplication/StatefulBackendService