Skip to content

Instantly share code, notes, and snippets.

@Thorium
Created March 4, 2012 10:48
Show Gist options
  • Save Thorium/1972368 to your computer and use it in GitHub Desktop.
Save Thorium/1972368 to your computer and use it in GitHub Desktop.
Silverlight asynchronous WebService call with UI-thread syncronization dispatcher
//#r "System.Runtime.Serialization"
//#r "FSharp.PowerPack"
open Microsoft.FSharp.Control.WebExtensions
open System
open System.Runtime.Serialization
open System.Runtime.Serialization.Json
open System.Net
open System.IO
let internal fetchAsync (url : Uri) trigger =
let req = WebRequest.CreateHttp url
req.CookieContainer <- new CookieContainer()
let asynccall =
async{
try
let! res = req.AsyncGetResponse()
use stream = res.GetResponseStream()
use reader = new StreamReader(stream)
let! rdata = reader.AsyncReadToEnd()
callresult <- rdata //some processing like unjson here...
trigger "" |> ignore
with
| _ as ex -> //for debug
failwith(ex.ToString())
}
asynccall |> Async.StartImmediate
//UI-thread syncronization with Dispatcher
let trigger _ =
let update _ = x.MyViewModelProperty <- callresult
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(new Action(update)) |> ignore
let service = new Uri("http://...", UriKind.Absolute)
fetchAsync service trigger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment