我很难理解为什么有些代码永远不会被执行。
考虑这种扩展方法:
type WebSocketListener with
member x.AsyncAcceptWebSocket = async {
try
let! client = Async.AwaitTask <| x.AcceptWebSocketAsync Async.DefaultCancellationToken
if(not (isNull client)) then
return Some client
else
return None
with
| :? System.Threading.Tasks.TaskCanceledException ->
| :? AggregateException ->
return None
}
我知道取消令牌取消时AcceptSocketAsync
会抛出TaskCanceledException
。我已经检查过C#应用程序。想法是返回None
。
但是,这种情况从未发生过。如果我在最后return None
或甚至if
表达式中放置断点,则取消令牌取消时它永远不会停在那里。我知道它正在Async.AwaitTask
中等待,因为如果在取消之前,其他客户端连接,它可以工作,并在断点处停止。
我有点迷失,为什么异常会丢失?
答案 0 :(得分:3)
取消使用F#asyncs中的特殊路径 - Async.AwaitTask将取消任务的执行重新路由到取消继续。如果您想要不同的行为 - 您可以随时手动执行此操作:
type WebSocketListener with
member x.AsyncAcceptWebSocket = async {
let! ct = Async.CancellationToken
return! Async.FromContinuations(fun (s, e, c) ->
x.AcceptWebSocketAsync(ct).ContinueWith(fun (t: System.Threading.Tasks.Task<_>) ->
if t.IsFaulted then e t.Exception
elif t.IsCanceled then s None // take success path in case of cancellation
else
match t.Result with
| null -> s None
| x -> s (Some x)
)
|> ignore
)
}