Skip to content

Instantly share code, notes, and snippets.

@thaianhduc
Created October 1, 2018 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thaianhduc/52e8a77669e6d1356ac38d8d703b214a to your computer and use it in GitHub Desktop.
Save thaianhduc/52e8a77669e6d1356ac38d8d703b214a to your computer and use it in GitHub Desktop.
Simple extension with delegate
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