HttpClient SendAsync hang during first call when my application start. Need help.

I’m writing a library to call a REST web service using .NET 4.6.1 framework. One of the method is a simple GET method using SendAsync from HttpClient class. There is a strange behavior i observed when running my application in production environment. For your info about my production environment, the server and client are in the same building but different area, both machines are connected through LAN.

When my client application start (its a WinForm by the way), first SendAsync call tends to hang for about 2 minutes before the SendAsync throw “task is cancel” error. I can only reproduce this error by repeatedly kill and restart my application, maybe out of 20-30 tries this error will appear once. When the error appears, i checked netstat for both server and client, there are no established TCP connection between them.

Google search results suggest to add CancellationTokenSource to SendAsync and use CancelAfter method to specify a shorter timeout for SendAsync call. I tried to add a 30 seconds timeout but it won’t cancel the call after 30 seconds, it waited for around 2 minutes only then my application catch the “task is cancel” error.

There are suggestions to write a singleton class that instantiate HttpClient once and reuse it in the application. Tried that too but the issue still persists.

Anyone experienced similar issue like mine before? I’m out of idea now, had never experience this issue before for my other project that utilize HttpClient. I’d really appreciate if someone can point me to the right direction.

submitted by /u/lycan2005
[link] [comments]

Leave a Reply