在Async.Start中捕获异常?

ca9*_*3d9 6 f#

我有以下代码.我想在不阻塞主线程的情况下运行.

let post () = .....
try
    let response = post ()
    logger.Info(response.ToString())
with 
| ex -> logger.Error(ex, "Exception: " + ex.Message)
Run Code Online (Sandbox Code Playgroud)

所以我将代码更改为以下内容.但是,如何捕获异常post呢?

let post = async { 
   ....
   return X }
try
    let response = post |> Async.StartChild
    logger.Info(response.ToString())
with 
| ex -> logger.Error(ex, "Exception: " + ex.Message)
Run Code Online (Sandbox Code Playgroud)

Dax*_*ohl 1

async你也可以将 try/catch 放在一个块中

let post = async { .... }
async {
  try
    let! response = post
    logger.Info(response.ToString())
  with 
  | ex -> logger.Error(ex, "Exception: " + ex.Message)
} |> Async.Start
Run Code Online (Sandbox Code Playgroud)