Ho scritto una logica molto riprovevole per le chiamate client DocumentDB limitate.DocumentDB TransientFaultHandling Best Practice
L'esempio seguente è un esempio comune di questo con 10 tentativi.
La mia domanda è doppia: Questa è la migliore pratica, e c'è un modo meno dettagliato per gestirlo? Vedo che esiste un pacchetto nuget di Microsoft.Azure.Documents.Client.TransientFaultHandling pacchetto Microsoft n. che dovrebbe ottenere gli stessi risultati con meno codice, ma non riesco a trovare alcun esempio su StackOverflow o Google e sembra che non ci sia alcun chiaro documentazione disponibile da Microsoft.
int maxRetryAttempts = 10;
while (maxRetryAttempts > 0)
{
try
{
// Attempt to call DocumentDB Method
// ---[DocumentDB Method Here]---
}
catch (DocumentClientException de)
{
if (de.StatusCode.HasValue)
{
var statusCode = (int)de.StatusCode;
if (statusCode == 429 || statusCode == 503)
{
//Sleep for retry amount
Thread.Sleep(de.RetryAfter);
//Decrement max retry attempts
maxRetryAttempts--;
}
}
}
catch (AggregateException ae)
{
foreach (Exception ex in ae.InnerExceptions)
{
if (ex is DocumentClientException)
{
var documentClientException = ex as DocumentClientException;
var statusCode = (int)documentClientException.StatusCode;
if (statusCode == 429 || statusCode == 503)
{
//Sleep for retry amount
Thread.Sleep(documentClientException.RetryAfter);
//Decrement max retry attempts
maxRetryAttempts--;
}
else
{
throw;
}
}
}
}
}
if (maxRetryAttempts < 0)
{
//Max retry attempts reached
}
In il tuo blocco 'catch', quando si estrae l'oggetto 'AggregateException', scarta silenziosamente tutte le eccezioni non-'DocumentClientException'. È intenzionale? Mi sarei aspettato bolle di eccezioni sconosciute. –