public Task <Result <Unit> > RetryStopService(string name, int maxAttempts, TimeSpan retryInterval) { return(Retry.GetAsync( getValue: () => GetService(name) .MapAsync(s => { if (s.Status == ServiceControllerStatus.Stopped) { return true; } else { s.Stop(); return false; } }), predicate: result => result.HasValue && result.Value, getErrorMessage: result => result.HasValue ? "Timed out without getting an acceptable value." : $"Timed out with exception: '{result.Error.Message}'", maxAttempts: maxAttempts, interval: retryInterval) .IgnoreAsync()); }
public Task <Result <Unit> > RetryStartService(string name, int maxAttempts, TimeSpan retryInterval) { const string alreadyRunning = "An instance of the service is already running"; return(Retry.GetAsync( getValue: () => GetService(name) .MapAsync(s => { if (s.Status == ServiceControllerStatus.Running) { return true; } else { s.Start(); return false; } }), predicate: result => (result.HasValue && result.Value) || (!result.HasValue && result.Error.InnerException.Message == alreadyRunning), getErrorMessage: result => result.HasValue ? "Timed out without getting an acceptable value." : $"Timed out with exception: '{result.Error.Message}'", maxAttempts: maxAttempts, interval: retryInterval) .IgnoreAsync() .CatchAsync(er => er.InnerException.Message == alreadyRunning, er => Unit.Value.AsResult())); }