Created
October 1, 2018 08:35
-
-
Save thaianhduc/52e8a77669e6d1356ac38d8d703b214a to your computer and use it in GitHub Desktop.
Simple extension with delegate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ServiceCallHelper | |
{ | |
/// <summary> | |
/// Allow consumers to define the log exception action right before each call | |
/// </summary> | |
public Action<Exception> LogException { get; set; } | |
public TResult CallWithRetry<TResult>(Func<TResult> realCall, bool retry = true) | |
{ | |
try | |
{ | |
return realCall(); | |
} | |
catch (TimeoutException te) | |
{ | |
Log(te); | |
if (retry) | |
return Retry(realCall); | |
throw; | |
} | |
} | |
private void Log(Exception exception) | |
{ | |
LogException?.Invoke(exception); | |
} | |
private TResult Retry<TResult>(Func<TResult> realCall) | |
{ | |
try | |
{ | |
return realCall(); | |
} | |
catch (Exception e) | |
{ | |
Log(e); | |
throw; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment