PageRenderTime 46ms CodeModel.GetById 33ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 2ms

/iBlue.ResxHelper/Src/AsyncHelpers.fs

http://github.com/fahadsuhaib/FSharpTools
F# | 51 lines | 27 code | 12 blank | 12 comment | 2 complexity | 5835a3333a4b9333dd9cd4c7b3689d34 MD5 | raw file
 1namespace FSharp.Tutorial
 2
 3open System.Text
 4open System.IO
 5open Microsoft.FSharp.Control.WebExtensions
 6
 7[<AutoOpen>]
 8module AsyncHelpers =
 9
10    type System.IO.StreamReader with 
11
12        /// An extension member to read all the lines from a stream reader asynchronously.
13        /// 
14        /// In this implementation, the operation is pseduo-asynchronous.
15        member reader.AsyncReadAllLines () = 
16            async { return [ while not reader.EndOfStream do 
17                                yield reader.ReadLine()] }
18    
19    type System.Net.WebRequest with
20
21
22        /// An extension member to write content into an WebRequest. 
23        /// 
24        /// In this implementation, the operation is pseduo-asynchronous.
25        member req.AsyncWriteContent (content:string) = 
26            async { let bytes = Encoding.UTF8.GetBytes content
27                    req.ContentLength <- int64 bytes.Length
28                    use stream = req.GetRequestStream()
29                    stream.Write(bytes,0,bytes.Length)  }
30
31        /// An extension member to read all the content of a response to a WebRequest. 
32        /// 
33        /// In this implementation, the operation is pseduo-asynchronous.
34        member req.AsyncReadResponse () = 
35            async { use! response = req.AsyncGetResponse()
36                    use responseStream = response.GetResponseStream()
37                    use reader = new StreamReader(responseStream)
38                    return reader.ReadToEnd() }
39
40        /// An extension member to read all the content of a response to a WebRequest as a set of lines.
41        /// 
42        /// In this implementation, the operation is pseduo-asynchronous.
43        member req.AsyncReadResponseLines () = 
44            async { use! response = req.AsyncGetResponse()
45                  
46                    return [use stream = response.GetResponseStream()
47                            use reader = new StreamReader(stream)
48                            while not reader.EndOfStream do 
49                                yield reader.ReadLine()] }
50
51