in generators/CoreCLRStatefulService/templates/service/class/ServiceImpl.cs [45:74]
protected override async Task RunAsync(CancellationToken cancellationToken)
{
// TODO: Replace the following sample code with your own logic
// or remove this RunAsync override if it's not needed in your service.
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");
var counterValue = await myDictionary.GetOrAddAsync(tx, "Counter", 0);
Console.WriteLine("Incrementing counter... {0} {1} : {2}", this.myName, this.myId, counterValue);
ServiceEventSource.Current.Message("Incrementing counter from replica : " + this.myId + " : current value : " + counterValue);
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);
}
}