[英]Unifying Task<T> and F# MailboxProcessor exception handling
使用Task <T>時,在Task.Wait()期間拋出任務執行期間的異常; 當使用F#的MailBoxProcessor時,異常會被吞下,需要根據這個問題明確處理。
這種差異使得很難通過Task將F#代理暴露給C#代碼。 例如,此代理:
type internal IncrementMessage =
Increment of int * AsyncReplyChannel<int>
type IncrementAgent() =
let counter = Agent.Start(fun agent ->
let rec loop() = async { let! Increment(msg, replyChannel) = agent.Receive()
match msg with
| int.MaxValue -> return! failwith "Boom!"
| _ as i -> replyChannel.Reply (i + 1)
return! loop() }
loop())
member x.PostAndAsyncReply i =
Async.StartAsTask (counter.PostAndAsyncReply (fun channel -> Increment(i, channel)))
可以從C#調用,但異常不會返回給C#:
[Test]
public void ExceptionHandling()
{
//
// TPL exception behaviour
//
var task = Task.Factory.StartNew<int>(() => { throw new Exception("Boom!"); });
try
{
task.Wait();
}
catch(AggregateException e)
{
// Exception available here
Console.WriteLine("Task failed with {0}", e.InnerException.Message);
}
//
// F# MailboxProcessor exception behaviour
//
var incAgent = new IncrementAgent();
task = incAgent.PostAndAsyncReply(int.MaxValue);
try
{
task.Wait(); // deadlock here
}
catch (AggregateException e)
{
Console.WriteLine("Agent failed with {0}", e.InnerException.Message);
}
}
C#代碼只掛起在task.Wait()而不是獲得異常。 有沒有辦法讓F#代理行為像任務? 如果沒有,似乎將F#代理暴露給其他.NET代碼的用途有限。
處理它的一種方法是讓代理返回帶有錯誤情況的DU。 然后,您可以從代理外部引發異常。
type internal IncrementResponse =
| Response of int
| Error of exn
type internal IncrementMessage =
| Increment of int * AsyncReplyChannel<IncrementResponse>
type IncrementAgent() =
let counter = Agent.Start(fun agent ->
let rec loop() =
async {
let! Increment(msg, replyChannel) = agent.Receive()
match msg with
| int.MaxValue -> replyChannel.Reply (Error (Failure "Boom!"))
| _ as i -> replyChannel.Reply (Response(i + 1))
return! loop()
}
loop())
member x.PostAndAsyncReply i =
Async.StartAsTask (
async {
let! res = counter.PostAndAsyncReply (fun channel -> Increment(i, channel))
match res with
| Response i -> return i
| Error e -> return (raise e)
}
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.