我正在尝试实现Azure Service Fabric Reliable Collection的最简单用法--- Hello World
示例,可以说是 --- IReliableDictionary
。
在 this link给出了一个例子。然而这个例子需要一个 StateManager 对象,我不确定如何创建它,甚至不确定它是什么。这似乎是Actor是需要的,我现在希望避免使用 Actor。
谁能举个例子吗?
提前非常感谢
最佳答案
没关系,我找到了答案。
关键的一点是,无法在无状态服务中创建可靠的集合,如果我们想要使用有状态服务,我们就必须创建一个有状态服务 em>可靠的收藏。
Here我找到了可靠字典的实现示例。接下来我粘贴代码,该代码应位于 MyStatefulService.cs
类中:
protected override async Task RunAsync(CancellationToken cancellationToken)
{
var myDictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, long>>("myDictionary");
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
using (var tx = this.StateManager.CreateTransaction())
{
var result = await myDictionary.TryGetValueAsync(tx, "Counter");
ServiceEventSource.Current.ServiceMessage(this, "Current Counter Value: {0}",
result.HasValue ? result.Value.ToString() : "Value does not exist.");
await myDictionary.AddOrUpdateAsync(tx, "Counter", 0, (key, value) => ++value);
// If an exception is thrown before calling CommitAsync, the transaction aborts, all changes are
// discarded, and nothing is saved to the secondary replicas.
await tx.CommitAsync();
}
await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
}
关于azure - 最简单的 Service Fabric 可靠集合(字典)示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44681498/