在异步工作流中捕获HttpClient超时

时间:2016-05-09 09:55:07

标签: f# dotnet-httpclient f#-async

我通过HttpClient呼叫Async.AwaitTask,从代理(MailboxProcessor)中调用。我想在HTTP调用期间捕获错误,因此在异步工作流中使用try...with,但它完全错过捕获客户端超时异常,然后导致代理崩溃。

最小复制:

#r "System.Net.Http"
open System
open System.Net.Http

let client = new HttpClient()
client.Timeout <- TimeSpan.FromSeconds(1.)
async {
    try
        let! content = Async.AwaitTask <| client.GetStringAsync("http://fake-response.appspot.com/?sleep=30")
        return content
    with ex ->
        // Does not catch client-side timeout exception
        return "Caught it!"
}
|> Async.RunSynchronously
// Throws System.OperationCanceledException: The operation was canceled

我可以通过使其完全同步来修复它,但更愿意保持整个堆栈的异步,因为可能并行运行大量这些:

#r "System.Net.Http"
open System
open System.Net.Http

let client = new HttpClient()
client.Timeout <- TimeSpan.FromSeconds(1.)
try
    Async.AwaitTask <| client.GetStringAsync("http://fake-response.appspot.com/?sleep=30")
    |> Async.RunSynchronously
with ex ->
    "Caught it!"
// Returns "Caught it!"

是否有一种在异步上下文中捕获OperationCanceledException的有效方法?

1 个答案:

答案 0 :(得分:7)

这是因为HttpClient.GetStringAsync任务将被取消,而不是使用TimeoutException失败,从而提示异步机制触发其取消延续,这是无法处理的。解决此问题的一种简单方法如下:

async {
    try
        let! content = 
            client.GetStringAsync("http://fake-response.appspot.com/?sleep=30")
                  .ContinueWith(fun (t:Task<string>) -> t.Result)
            |> Async.AwaitTask
        return content
    with ex ->
        // Does not catch client-side timeout exception
        return "Caught it!"
}