0

I found this peculiar behaviour of UWP HttpClient.

For a simple get call of WCF service is taking time almost more than 100 seconds(happens for first few calls). I observed that the same service is way too faster in iOS and rest client.

Attaching the code, Please let me know, if i doing wrong.

            HttpClientHandler clientHandler = new HttpClientHandler();
            clientHandler.UseCookies = true;

            var client = new HttpClient(clientHandler);
            client.Timeout = TimeSpan.FromSeconds(100);

            client.DefaultRequestHeaders.Add("Accept", "application/json");

            var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, 
            requestUri);
            var response = await client.SendAsync(httpRequestMessage);

            //Between these lines there is a delay, it doesnt fire time out 
            also. I tried with HttpCompletionOption. No luck

            if (response.IsSuccessStatusCode)
            {
                var content = await response.Content.ReadAsStringAsync(); 

            }

Any help will be really appreciated.

Thanks and Regards,

Rummy

user998644
  • 11
  • 1
  • Please provide a *complete* example. Also please give some context. Could it be just that the server is not responding and you're hitting the timeout? Also, it looks here like you're creating the `HttpClient` every time you want to make a request, which will not scale. `HttpClient` should be a singleton in your app. See [these docs from Microsoft](https://learn.microsoft.com/en-us/azure/architecture/antipatterns/improper-instantiation/). – Matt Johnson-Pint Jun 24 '17 at 22:23
  • Does `ServicePointManager.UseNagleAlgorithm = false;` before making the call speed it up? – mjwills Jun 24 '17 at 23:01
  • client.Timeout dont make it time out. – lindexi Jun 25 '17 at 02:12
  • If you restart the WCF service, and then access it from iOS (so the first request is from iOS) does it also take 100+ seconds? – mjwills Jun 25 '17 at 07:54
  • No it doesnt take 100+ seconds in iOS for first time – user998644 Jun 26 '17 at 18:27
  • Matt Johnson, Thanks for the reply. I need to make multiple service requests at on launch of the application. This will happen using background Task. Singleton HttpClient doesnt suit my requirement. – user998644 Jun 26 '17 at 18:28
  • You can use a single `HttpClient` from multiple threads at once, that should still work. Are you using `Windows.Web.Http.HttpClient` or the `System.Net.Http.HttpClient`. While the second one is cross-platform, the first one should be more optimized for UWP – Martin Zikmund Jun 27 '17 at 17:12

0 Answers0