public async Task StopAsync()
{
IReliableDictionary<string, CurrentState> chaosServiceState =
await this.StateManager.GetOrAddAsync<IReliableDictionary<string, CurrentState>>(StringResource.ChaosServiceStateKey);
bool shouldCancel = false;
using (ITransaction tx = this.StateManager.CreateTransaction())
{
ConditionalValue<CurrentState> currentStateResult =
await chaosServiceState.TryGetValueAsync(tx, StringResource.ChaosServiceStateKey, LockMode.Update);
if (currentStateResult.HasValue &&
currentStateResult.Value == CurrentState.Running)
{
await
chaosServiceState.AddOrUpdateAsync(
tx,
StringResource.ChaosServiceStateKey,
CurrentState.Stopped,
(key, existingValue) => CurrentState.Stopped);
shouldCancel = true;
}
await tx.CommitAsync();
}
if (shouldCancel && this.stopEventTokenSource != null && !this.stopEventTokenSource.IsCancellationRequested)
{
this.stopEventTokenSource.Cancel();
}
}