Flurl引发FlurlHttpException,而不是FlurlHttpTimeoutException



我需要将超时作为一种特殊情况来处理,但在某些情况下,flurl不会给我预期的异常。

这是我的简化代码

try
{           
// Call Listener here with the result of the process
using (IFlurlClient flurlClient = new FlurlClient(Url))
{
string response = await flurlClient
.Configure(s => s.Timeout = TimeSpan.FromSeconds(130))
.WithOAuthBearerToken(accessToken)
.Request()
.PostJsonAsync(result)
.ReceiveString();
Logger.LogInformation(response);
ListenerResponse listenerResponse = JsonConvert.DeserializeObject<ListenerResponse>(response);
Logger.LogInformation(listenerResponse.Success.ToString());
if (!listenerResponse.Success)
{
throw new RejectedException("Listener rejected the request.");
}
}
}
catch (FlurlHttpTimeoutException ex)
{
// throws with this message: "Call timed out: POST https://..."
Logger.LogError(ex, $"Could not reach to Listener at: {Url}.");
throw;
}
catch (FlurlHttpException ex)
{
// throws with this message: "Call failed. Connection timed out POST https://..."
Logger.LogError(ex, ex.Message); 
throw;
}

发生的情况是,当我将超时设置为低于某个值(<130(时,flurl抛出预期的FlurlHttpTimeoutException。但是,如果我将超时设置为高于该值(>=130(,则flurl这次会抛出更通用的FlurlHttpException

请注意,异常消息略有不同:

FlurlHttpTimeoutException

调用超时:POSThttps://...

FlurlHttpException

调用失败。连接超时POSThttps://...

有人知道我如何修复它以使其按预期运行吗?为所有超时值抛出FlurlHttpTimeoutException

我最终做了这样的事情来解决这个问题,不确定这是否是正确的方法,但它做到了。

/// <summary>
/// Gets a flurl client
/// </summary>
/// <returns></returns>
private IFlurlClient GetFlurlClient()
{
return new FlurlClient(new HttpClient(new SocketsHttpHandler
{
PooledConnectionLifetime = TimeSpan.FromSeconds(ListenerTimeout + 10)
})
{
BaseAddress = new Uri(ListenerUrl)
})
.Configure(s => s.Timeout = TimeSpan.FromSeconds(ListenerTimeout));
}

相关内容

  • 没有找到相关文章

最新更新