F # asynchronous web request exception handling

I am trying to use asynchronous workflows in F # to receive multiple web requests.

However, some of my requests sometimes return errors (e.g. http 500), and I don't know how to handle this. It looks like my F # program is stuck in an endless loop when working in the debugger.

I probably miss some things because the examples I saw did not compile out of the box. The first thing I found helped in this bit of code:

type System.Net.WebRequest with
  member req.GetResponseAsync() =
    Async.BuildPrimitive(req.BeginGetResponse, req.EndGetResponse)

and then I have my own piece of code to extract the queries, which is pretty standard from the examples I saw:

let async_value = async {
  let req = WebRequest.Create(url)
  let! rsp = req.GetResponseAsync()
  return (rsp :?> HttpWebResponse).StatusCode
}

and then I will try to get the result:

let status = Async.RunSynchronously(async_value)

, req.EndGetResponse, 500. , , req.EndGetResponse ( ), status = Async.RunSynchronously (async_value).

, ? , , ? /dll F #/VS 2010 Beta 1, ?

, Async.RunSynchronously (Async.Parallel (my_array_of_async_values)), , , .

, , , Async.Run, Async.RunSynchronously, , , ... =/

+5
2

"AsyncGetResponse" ( "GetResponseAsync" ). "Run" "RunSynchronously". , , .

"\\\\ " "\" (, , , CLR )? , VS- ( ). , / "" F # Beta1 , async, , , , , ..

VS2008 CTP VS2010 Beta1?

- 500, WebRequest. :

open System
open System.ServiceModel 
open System.ServiceModel.Web 

[<ServiceContract>]
type IMyContract =
    [<OperationContract>]
    [<WebGet(UriTemplate="/Returns500")>]
    abstract Returns500 : unit -> unit
    [<OperationContract>]
    [<WebGet(UriTemplate="/Returns201")>]
    abstract Returns201 : unit -> unit

type MyService() =
    interface IMyContract with
        member this.Returns500() =
            WebOperationContext.Current.OutgoingResponse.StatusCode <- 
                System.Net.HttpStatusCode.InternalServerError 
        member this.Returns201() =
            WebOperationContext.Current.OutgoingResponse.StatusCode <- 
                System.Net.HttpStatusCode.Created 

let addr = "http://localhost/MyService"
let host = new WebServiceHost(typeof<MyService>, new Uri(addr))
host.AddServiceEndpoint(typeof<IMyContract>, new WebHttpBinding(), "") |> ignore
host.Open()

open System.Net

let url500 = "http://localhost/MyService/Returns500"
let url201 = "http://localhost/MyService/Returns201"
let async_value (url:string) = 
    async {  
        let req = WebRequest.Create(url)  
        let! rsp = req.AsyncGetResponse()  
        return (rsp :?> HttpWebResponse).StatusCode
    }
let status = Async.RunSynchronously(async_value url201)
printfn "%A" status
try
    let status = Async.RunSynchronously(async_value url500)
    printfn "%A" status
with e ->
    printfn "%s" (e.ToString())
+2

try... async :

let async_value =
    async {
        let req = WebRequest.Create("http://unknown")
        try
            let! resp = req.AsyncGetResponse()
            return "success"
        with
        |   :? WebException as e -> return "failure"
    }
+1

All Articles