F # Async.FromBeginEnd do not catch exceptions

I am having problems with why the following code does not throw an exception. This is my first transition from Async to F #, so I'm sure this is 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 ran this and the table has already been created, it says that an exception of type StorageClientException was not handled in the code, in particular, pointing to the client part of the .EndCreateTable call from FromBeginEnd

+4
source share
3 answers

Thanks to Don Sim, the solution is to disable the "Only my code" debugging. Debugging β†’ Settings and Settings β†’ General β†’ uncheck the box β€œInclude only my code (only managed)

This is still a problem with the beta version of Visual Studio 11, which appeared with a preview of Windows 8.

+2
source

This smells like a problem that was fixed in FSharp.Core in VS2010 SP1..NET SynchronizationContext changed its behavior (in .NET 4.0 SP1, I think), and we needed a corresponding change in the F # runtime for asynchronous interaction to correctly cope with the affinity of the stream.

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

+4
source

If the reason for the exception is because the table already exists, why not use BeginCreateTableIfNotExist / EndCreateTableIfNotExist .

UPDATE:

The error has nothing to do with Windows Azure. I can reproduce the same behavior with a simple program:

 open System open System.Windows.Forms let bufferData = Array.zeroCreate<byte> 100000000 let async1 filename = async{ try use outputFile = System.IO.File.Create(filename) do! outputFile.AsyncWrite(bufferData) MessageBox.Show("OK") |> ignore with | :? ArgumentException -> printfn "Failed with ArgumentException"; MessageBox.Show("Failed with ArgumentException") |> ignore | _ -> printfn "Failed with unknown exception"; MessageBox.Show("Failed with unknown exception") |> ignore } |> Async.Start let main(args) = let form = new Form(Text = "Test Form") let button1 = new Button(Text = "Start") let button2 = new Button(Text = "Start Invalid", Top = button1.Height + 10) form.Controls.AddRange [| button1; button2; |] button1.Click.Add(fun args -> async1 "longoutput.dat") // Try an invalid filename to test the error case. button2.Click.Add(fun args -> async1 "|invalid.dat") let result = form.ShowDialog() 0 let _ = main([||]) 

It is strange that the code works fine in F # Interactive, but it is not possible to catch exceptions when debugging a Windows application inside Visual Studio (it does not matter if you debug or configure Release). Even a stranger, it works great if run as an application outside of Visual Studio.

In case you are interested, this program is adapted from the MSDN example , which shows the same problem.

UPDATE 2:

A similar question was asked at http://cs.hubfs.net/topic/Some/0/59516 . As @ildjarn and @Brian noted, this bug was fixed in VS2010 SP1. Without VS2010 SP1, you can also test your code with F # Interactive and run the application outside of VS without any problems.

+2
source

Source: https://habr.com/ru/post/1389196/


All Articles