我遇到了困难,不知道为什么下面的代码没有捕获异常。这是我第一次使用F#中的异步操作,我相信这是一个简单的问题。
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
如果我运行这个程序并且表已经被创建了,它会显示一个 StorageClientException 类型的异常未在代码中处理,具体指向 FromBeginEnd 调用的 client.EndCreateTable 部分。
MessageBox.Show
不是一个好主意。 - Joel MuellerAsync.Start
替换为Async.RunSynchronously
,会有什么不同吗? - Joel Mueller