Skip to content

Instantly share code, notes, and snippets.

@danielplawgo
Created September 2, 2018 04:16
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 danielplawgo/02852a0ced727620839a4dadfc2f611d to your computer and use it in GitHub Desktop.
Save danielplawgo/02852a0ced727620839a4dadfc2f611d to your computer and use it in GitHub Desktop.
Jak ponawiać operacje w .NET z wykorzystaniem Polly?
outbound and ip.DstAddr >= 185.255.40.24 and ip.DstAddr <= 185.255.40.24
class Program
{
private static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
_logger.Info("Start");
try
{
var content = Download("http://plawgo.pl");
_logger.Info("Success");
}
catch (Exception ex)
{
_logger.Fatal(ex, "Error");
}
_logger.Info("End");
}
static string Download(string url)
{
var client = new WebClient();
return client.DownloadString(url);
}
}
static string DownloadWithRetry(string url)
{
var client = new WebClient();
return Policy
.Handle<WebException>()
.Retry(3, (ex, retryCount) =>
{
_logger.Error(ex, $"Error - try retry (count: {retryCount})");
})
.Execute(() => client.DownloadString(url));
}
static string DownloadWithRetryAndDelay(string url)
{
var client = new WebClient();
return Policy
.Handle<WebException>()
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(10),
TimeSpan.FromSeconds(20),
TimeSpan.FromSeconds(50)
}, (ex, timeSpan, retryCount, context) =>
{
_logger.Error(ex, $"Error - try retry (count: {retryCount}, timeSpan: {timeSpan})");
})
.Execute(() => client.DownloadString(url));
}
static string DownloadWithRetryForever(string url)
{
var client = new WebClient();
return Policy
.Handle<WebException>()
.RetryForever(ex =>
{
_logger.Error(ex, $"Error - try retry forever");
})
.Execute(() => client.DownloadString(url));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment