Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# Async.FromBeginEnd not catching exceptions

I'm having trouble working out why the following code doesn't catch the exception. It's my first go with Async in F# so I'm sure it's something simple

open System
open Microsoft.WindowsAzure
open Microsoft.WindowsAzure.StorageClient
open System.Windows.Forms

let mutable connection = "UseDevelopmentStorage=true;DevelopmentStorageProxyUri=http://ipv4.fiddler"

CloudStorageAccount.SetConfigurationSettingPublisher(fun cName cPublisher ->
                                                      cPublisher.Invoke connection |> ignore)

let storageAccount = CloudStorageAccount.Parse connection

let createTable tableName =
        let client = storageAccount.CreateCloudTableClient()
        async{
            try
                do! Async.FromBeginEnd(tableName, client.BeginCreateTable , client.EndCreateTable)
                MessageBox.Show "Created" |>ignore
            with 
            | :? StorageClientException -> printfn "failed"; MessageBox.Show("failed to create table") |> ignore
            | _ -> printfn "Failed with unknown exception"
        } |> Async.Start

[<EntryPoint; STAThread>]
let main(args) =
    let form = new Form()
    let btn = new Button(Text = "Click")
    btn.Click.AddHandler(fun _ _ -> createTable "SomeNewTable")
    form.Controls.Add btn
    let result = form.ShowDialog()
    0

If I run this and the table has already been created it says that an exception of type StorageClientException was not handled in the code, specifically pointing at the client.EndCreateTable part of the FromBeginEnd call

like image 870
Dylan Avatar asked Feb 22 '23 23:02

Dylan


1 Answers

This smells like an issue that was fixed in FSharp.Core in VS2010 SP1. The .NET SynchronizationContexts changed their behavior (in .NET 4.0 SP1, I think), and we needed a corresponding change in the F# runtime for async's to properly deal with the thread affinity.

I think you can grab the newer FSharp.Core here: http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=15834

like image 177
Brian Avatar answered Mar 07 '23 13:03

Brian