0

コードが 429 問題の WebException にヒットしたという事実を知っているのに、ポリシー ハンドルが WebException エラーを処理していない理由を教えてもらえますか? 私はこれをしばらく研究しており、助けが必要です。

これは私のポリー ポリシーです。

return Policy.Handle<WebException>()
            .WaitAndRetry(15, // We can also do this with WaitAndRetryForever... but chose WaitAndRetry this time.
            attempt => TimeSpan.FromSeconds(0.1 * Math.Pow(2, attempt)), // Back off!  2, 4, 8, 16 etc times 1/4-second
            (exception, calculatedWaitDuration) =>  // Capture some info for logging! if needed
            {
                // This is your new exception handler! 
                Debug.WriteLine("Retry count: " + retries++);
                Debug.WriteLine("Wait Duration: " + calculatedWaitDuration);
            });

私はこのように使用しています:

 webResponseWaitRetryPolicy.Execute(() =>
        {
            // Make a request and get a response
            UriBuilder builder = new UriBuilder(options.BaseUrl);
                builder.Port = -1;
                var query = HttpUtility.ParseQueryString(builder.Query);
                /// handle parameters
                query["client_key"] = options.ClientKey;
                query["model_id"] = model;
                query["image_url"] = imageUrl;

                builder.Query = query.ToString();
                string url = builder.ToString();
                string content = string.Empty;

                Result objResult;
               HttpWebResponse response = null;


                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.AutomaticDecompression = DecompressionMethods.GZip;
                try
                {
                   response = (HttpWebResponse)request.GetResponse();
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        using (Stream stream = response.GetResponseStream())
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            content = reader.ReadToEnd();
                        }

                        objResult = JsonConvert.DeserializeObject<Result>(content);


                }
                catch(WebException ex)
                {
                    eventualFailures++; 
                    Debug.WriteLine("Failure: " + eventualFailures);


                }
            });

        return objResult;
    }

コードを実行すると、WebException がトリガーされていることがわかりますが、ポリシーは呼び出されていません。助けてください。

4

2 に答える 2

0

すべての助けに感謝します。問題は、WebException が 429 エラーであることです。そこに try catch メソッドを入れないと、コードが壊れてしまいます。したがって、私が最終的に行うことは、このように見えるようにポリシーをもう少し拡張することです。

>    public static Policy<HttpWebResponse> Get429WebResponseWaitRetryPolicy()
        {
            //Retries policy
            return Policy.Handle<WebException>().OrResult<HttpWebResponse>(r => r == null)
                .WaitAndRetry(15, // We can also do this with WaitAndRetryForever... but chose WaitAndRetry this time.
                attempt => TimeSpan.FromSeconds(0.1 * Math.Pow(2, attempt)), // Back off!  2, 4, 8, 16 etc times 1/4-second
                (exception, calculatedWaitDuration) =>  // Capture some info for logging! if needed
                {
                    // This is your new exception handler! 
                    Debug.WriteLine("Retry count: " + retries++);
                    Debug.WriteLine("Wait Duration: " + calculatedWaitDuration);
                });
        }

次に、API WebRequest からの応答を返します。Polly ポリシーは、スローされたエラーを使用して再試行できるようになりました。最終的にはこのようになります。

Debug.WriteLine("Success: " + eventualSuccesses);
                    return response;
                }
                catch(WebException ex)
                {
                    eventualFailures++; 
                    Debug.WriteLine("Failure: " + eventualFailures);
                    return response;
                 }
于 2018-12-17T14:04:24.593 に答える