Polly inside 任务超时

Polly inside task times out

我正在调用一个端点,如果它 returns 404 或在其他异常时立即失败,我想重试。

适用于较短的时间跨度,但当我 运行 具有指数退避时,我看到 "Scheduling attempt 10/11 in at [date]" 但没有进行后续调用。

我想在任务中 运行 因为我想将它从主线程中取出并在后台处理它。问题可能与正在处理的任务有关吗?

var settingsPage = _contentQueries.GetComponent<SettingsPage>();
var retryCount = 11;
var token = _authorizationService.GetBearerToken();

Task.Factory.StartNew(currentContext => {
    HttpContext.Current = (HttpContext) currentContext;

    var captureResult = Policy.HandleResult<HttpResponse>(r => r.Status == (int)HttpStatusCode.NotFound).WaitAndRetry(retryCount, retryAttempt =>
    {
        var nextAttemptInSeconds = Math.Pow(3, retryAttempt);
        _logger.Log($"Scheduling retry attempt {retryAttempt}/{retryCount} at {DateTime.Now.AddSeconds(nextAttemptInSeconds)}", activity, Level.Debug);
        return TimeSpan.FromSeconds(nextAttemptInSeconds);
    }).ExecuteAndCapture(() =>
    {
        var result = MakeWebClientGetRequest();

        if (RemoteErrorOccurred(result))
        {
            throw new Exception(result.Response);
        }

        return result;
    });


    if (captureResult.Outcome == OutcomeType.Successful)
    {
        _logger.Log("Process successful", activity, Level.Debug);
    }
    else
    {
        if (captureResult.FinalException != null)
        {
            _logger.Log($"Failed to process: {captureResult.FinalException}", activity, Level.Debug);
        }
        else
        {
            _logger.Log($"Failed to process after {retryCount} attempts", activity, Level.Debug);
        }
    }

    _databaseFactory.Dispose();
}, HttpContext.Current);
private static bool RemoteErrorOccurred(HttpResponse result)
{
    return result.Exception != null && result.Status != (int) HttpStatusCode.NotFound;
}

我最终使用 Hangfire's BackgroundJob.Enqueue 而选择了 Task.Factory.StartNew。应用程序池回收阻止了我问题中的 运行 任务。