Skip to main content
deleted 1 character in body
Source Link
Ami
  • 695
  • 4
  • 9

How about:

private const int TotalNumberOfAttempts = 10;

public static async Task<HttpResponseMessage> PutWithRetriesAsync(string url,
    HttpContent content,
    AuthenticationHeaderValue authenticationHeaderValue,
    MediaTypeWithQualityHeaderValue mediaTypeWithQualityHeaderValue)
{
    var exceptions = new List<Exceptions>List<Exception>();
    for(int i = 0; i < TotalNumberOfAttempts; i++)
    {
        try
        {
            return await PutAsync(url, content, authenticationHeaderValue, mediaTypeWithQualityHeaderValue);
        }
        catch (AggregateException ex)
        {
            exceptions.Add(ex);
        }
    } 
    throw new AggregateException(exceptions);
}

You can easily abstract this functionality into an extension method to provide retry logic for any arbitrary Func<Task>.

How about:

private const int TotalNumberOfAttempts = 10;

public static async Task<HttpResponseMessage> PutWithRetriesAsync(string url,
    HttpContent content,
    AuthenticationHeaderValue authenticationHeaderValue,
    MediaTypeWithQualityHeaderValue mediaTypeWithQualityHeaderValue)
{
    var exceptions = new List<Exceptions>();
    for(int i = 0; i < TotalNumberOfAttempts; i++)
    {
        try
        {
            return await PutAsync(url, content, authenticationHeaderValue, mediaTypeWithQualityHeaderValue);
        }
        catch (AggregateException ex)
        {
            exceptions.Add(ex);
        }
    } 
    throw new AggregateException(exceptions);
}

You can easily abstract this functionality into an extension method to provide retry logic for any arbitrary Func<Task>.

How about:

private const int TotalNumberOfAttempts = 10;

public static async Task<HttpResponseMessage> PutWithRetriesAsync(string url,
    HttpContent content,
    AuthenticationHeaderValue authenticationHeaderValue,
    MediaTypeWithQualityHeaderValue mediaTypeWithQualityHeaderValue)
{
    var exceptions = new List<Exception>();
    for(int i = 0; i < TotalNumberOfAttempts; i++)
    {
        try
        {
            return await PutAsync(url, content, authenticationHeaderValue, mediaTypeWithQualityHeaderValue);
        }
        catch (AggregateException ex)
        {
            exceptions.Add(ex);
        }
    } 
    throw new AggregateException(exceptions);
}

You can easily abstract this functionality into an extension method to provide retry logic for any arbitrary Func<Task>.

Source Link
Ami
  • 695
  • 4
  • 9

How about:

private const int TotalNumberOfAttempts = 10;

public static async Task<HttpResponseMessage> PutWithRetriesAsync(string url,
    HttpContent content,
    AuthenticationHeaderValue authenticationHeaderValue,
    MediaTypeWithQualityHeaderValue mediaTypeWithQualityHeaderValue)
{
    var exceptions = new List<Exceptions>();
    for(int i = 0; i < TotalNumberOfAttempts; i++)
    {
        try
        {
            return await PutAsync(url, content, authenticationHeaderValue, mediaTypeWithQualityHeaderValue);
        }
        catch (AggregateException ex)
        {
            exceptions.Add(ex);
        }
    } 
    throw new AggregateException(exceptions);
}

You can easily abstract this functionality into an extension method to provide retry logic for any arbitrary Func<Task>.