【发布时间】:2020-04-29 09:47:33
【问题描述】:
我需要将超时作为特殊情况处理,但 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;
}
发生的情况是,当我将超时设置为低于某个值 () 时,Flurl 会抛出预期的 FlurlHttpTimeoutException。但是,如果我将超时设置为高于该值(>=130),这次 flurl 会抛出更通用的FlurlHttpException。
请注意,异常消息略有不同:
FlurlHttpTimeoutException
调用超时:POST https://...
FlurlHttpException
调用失败。 POST https://...
连接超时
有谁知道如何修复它以使其按预期运行 - 为所有超时值抛出 FlurlHttpTimeoutException?
【问题讨论】:
-
>130 时,FlurlHttpException.InnerException 的类型/详细信息是什么?另外,这是一个 Xamarin/Android 项目吗?
-
我偶然发现了让它工作的诀窍,那就是
SocketHttpHandler.PooledConnectionLifetime。当我将它增加到 130 以上时,它开始抛出正确的异常。这是一个适用于 aws lambdas 的 dotnet core 2.1 项目。