The small test app tries to communicate with the server using HttpClient, with the Timeout specified.
But it looks like the timeout never aborts the client request:
#r "System.Core.dll"
#r "System.Net.dll"
#r "System.Net.Http.dll"
#r "System.Web.dll"
#r "System.Web.Http.dll"
open System
open System.Net
open System.Net.Http
open System.Threading
open System.Web
open System.Web.Http
let listener = new HttpListener()
// https://stackoverflow.com/questions/4019466/httplistener-access-denied/4115328#4115328
// netsh http add urlacl url=http://*:7842/ user=COMPUTER\User
// netsh http show urlacl - make sure that it exists
// netsh http delete urlacl http://*:7842/ - delete it
listener.Prefixes.Add("http://*:7842/")
listener.Start()
let handler = async {
while true do
let context = listener.GetContext()
Console.WriteLine("ok")
}
let requester = async {
let client = new HttpClient()
client.Timeout <- TimeSpan.FromMilliseconds(float(1))
client.BaseAddress <- Uri("http://127.0.0.1:7842/")
try
let! result = client.GetAsync("") |> Async.AwaitTask
printf "result: %s" (string(result))
with
| _ -> printf "timeout?"
}
Async.Start(handler)
Thread.Sleep(750)
Async.Start(requester)
Thread.Sleep(100000)
The timeout never happen in this app which is very strange.
Update: It may be related to HttpClient.GetAsync(...) never returns when using await/async
Update2: It seems that this workaround is working but not sure if it correct:
try
let requestTask = client.GetAsync("")
let! success = Async.AwaitIAsyncResult(requestTask, 3000)
let result = requestTask.Result
printf "result: %s" (string(result))
with
| ex -> printf "timeout? %s\n" (ex.ToString())
Should the client be Disposed (probably not as it can be reused for another requests in other threads/tasks)?