0

我的要求是,如果请求成功,则它不能再次执行。但现在它每次都在调用:

public async Task Send(CancellationToken token)
{
    logger.LogInformation("E-mail background delivery started");

    while (!token.IsCancellationRequested)
    {

        try
        {
            if (FullUrl != null)
            {
                var request = new HttpRequestMessage(HttpMethod.Get, FullUrl);

                // Let's wait for a message to appear in the queue
                // If the token gets canceled, then we'll stop waiting
                // since an OperationCanceledException will be thrown

                var client = _clientFactory.CreateClient();

                // token.ThrowIfCancellationRequested();

                var response = await client.SendAsync(request, token).ConfigureAwait(false);

                if (!response.IsSuccessStatusCode)
                {
                    logger.LogInformation($"E-mail sent to");
                }
            }


            //as soon as a message is available, we'll send it
            logger.LogInformation($"E-mail sent to");
        }
        catch (OperationCanceledException)
        {
            //We need to terminate the delivery, so we'll just break the while loop
            break;
        }
        catch(Exception e)
        {
            #warning Implement a retry mechanism or else this message will be lost
            logger.LogWarning($"Couldn't send an e-mail to");
            break;
        }
    }

    logger.LogInformation("E-mail background delivery stopped");
}
4

1 回答 1

0

尝试下面的代码附加变量以指示 reposne 是否成功。

public async Task Send(CancellationToken token)
{
    logger.LogInformation("E-mail background delivery started");
    bool IsSuccess = false;
    while (!token.IsCancellationRequested && !IsSuccess)
    {

        try
        {
            if (FullUrl != null)
            {
                var request = new HttpRequestMessage(HttpMethod.Get, FullUrl);

                // Let's wait for a message to appear in the queue
                // If the token gets canceled, then we'll stop waiting
                // since an OperationCanceledException will be thrown

                var client = _clientFactory.CreateClient();

                // token.ThrowIfCancellationRequested();

                var response = await client.SendAsync(request, token).ConfigureAwait(false);

                if (!response.IsSuccessStatusCode)
                {
                    logger.LogInformation($"E-mail sent to");
                }
                else
                {
                    IsSuccess = true;
                }
            }


            //as soon as a message is available, we'll send it
            logger.LogInformation($"E-mail sent to");
        }
        catch (OperationCanceledException)
        {
            //We need to terminate the delivery, so we'll just break the while loop
            break;
        }
        catch(Exception e)
        {
            #warning Implement a retry mechanism or else this message will be lost
            logger.LogWarning($"Couldn't send an e-mail to");
            break;
        }
    }

    logger.LogInformation("E-mail background delivery stopped");
}
于 2019-04-01T02:51:17.763 回答