Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, April 10, 2020 3:04 PM
I recently updated my visual studio from 2017 to 2019. I am using HttpClient implementation as Default. Everything wass working very well in vs2017. After i update my vs to 2019. Then when i am using httpClient, timeout and Cancelation Token don't work.
Here is my code:
var token = cts.Token;
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromSeconds(20);
var response = await client.PostAsync("http://" + MyIp + ":9090/api/SqlAction/", new StringContent("my data", Encoding.UTF8, "application/json"), token);
if (response.IsSuccessStatusCode)
{
}
}
All replies (21)
Tuesday, April 14, 2020 10:23 AM âś…Answered
Yes, it should be. I simulate this kind of server by setting a 10 seconds delay before returning the "Hello World!". And the cancelationToken and timeout both work on my side. I suggest opening a free support ticket here: https://support.microsoft.com/en-us/supportforbusiness/productselection?sapId=211dd84f-3474-c3c5-79bf-66db630c92a6 You will get one-to-one support there as I really can't reproduce your issues here.
Monday, April 13, 2020 3:35 AM
What's the version of Xamarin.Android are you using? I tried to make a delayed server using the local environment and test it in Xamarin.Android like:
CancellationTokenSource cts = new CancellationTokenSource();
var token = cts.Token;
Button btn = FindViewById<Button>(Resource.Id.button);
btn.Click += async (sender, e) =>
{
try
{
HttpClient client = new HttpClient();
//client.Timeout = TimeSpan.FromSeconds(2);
var result = await client.GetAsync("http://localhost:3000/", token);
}
catch (System.Exception ex)
{
var builder = new Android.Support.V7.App.AlertDialog.Builder(this)
.SetMessage(ex.Message)
.SetPositiveButton("OK", (_, args) =>
{
})
.Show();
}
};
Button cancelBtn = FindViewById<Button>(Resource.Id.cancelButton);
cancelBtn.Click += (sender, e) =>
{
cts.Cancel();
};
Both timeout and cancellation work as expected: Here is the timeout:
Monday, April 13, 2020 9:46 PM
Hello Landlu here is my xamarin version
Unfortunately it doesn't work Please see video below
https://drive.google.com/file/d/1AZ7AJaquxfVkdXfHAz4gUrYpk9vYTaay/view?usp=sharing
It doesnt throw an exception
Tuesday, April 14, 2020 1:49 AM
I have the same settings as yours. Have you tried other simulators? Please post your sample here if possible. I need it to look into your issues on my side.
Tuesday, April 14, 2020 7:37 AM
Thank you LandLu. Here is my project
https://drive.google.com/file/d/12APCLF-\_w5uqoJCje1jyIXkcS8f4DVVC/view?usp=sharing
Also Timeout deosn't work neither. I tried the same thing in two different pc. One with windows 7 and another with Windows 10. Also i tried it in my phone, i mean without simulator. Same Project works fine in only in visual studio 2017.
Tuesday, April 14, 2020 7:58 AM
Still get the expected result: Was your request completed before canceling? Try to add an alert after request:
var result = await client.GetAsync("http://192.168.0.3:3000/", token);
var builder = new Android.Support.V7.App.AlertDialog.Builder(this)
.SetMessage("Success!")
.SetPositiveButton("OK", (_, args) =>
{
})
.Show();
to see whether this success alert will be presented.
Tuesday, April 14, 2020 8:01 AM
Still the same result from me. If i will set example timeout in 20 seconds. It will expire automatically after one minute about. It ignores token and timeout method. Can you please send me your project for trying it in my pc?
Tuesday, April 14, 2020 8:05 AM
I tested your project on my side. Could you please test it with other simulators or devices?
Tuesday, April 14, 2020 8:08 AM
I tested with two different simulators and 4 different physical devices. I am getting the same result. :neutral:
Tuesday, April 14, 2020 8:10 AM
What does your server code look like? I simulate a delayed request on the server part like:
app.get('/', (req, res) => {
setTimeout(() => {
res.send('Hello World!')
}, 10000);
})
So I can cancel the request within 10 seconds.
Tuesday, April 14, 2020 8:11 AM
My server is an httpListener. I can send you the code if you want. My server is not open when trying to use get async. Mobie should cancel the task right?
Tuesday, April 14, 2020 8:16 AM
What I mean is it could be probably caused by your server part. If your server code is too complicated it's not appropriate to post it here. You could try my simple test:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
setTimeout(() => {
res.send('Hello World!')
}, 10000);
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
If you have VS code, you could test it on your side.
Tuesday, April 14, 2020 8:20 AM
Thank you LundLu. Why do i need a server? Timeout and cancelation token should work without server, right? Same project in vs 2017 is work fine!
Tuesday, April 14, 2020 8:22 AM
Is there any case vs 2019 using new sdk versions or something like? Or it's propably deprecated managed as option in new vs 2019? But you said that is work in your machine
Tuesday, April 14, 2020 8:30 AM
Why do i need a server? Are you trying to use rest API to retrieve data from a server? Timeout and cancelation token should work without server, right? We need to simulate a server environment to create a several seconds delayed task on the server part. If the destination address returns the result in a very short time we can't trigger the
CancellationTokenSource
to cancel this request.
Tuesday, April 14, 2020 8:33 AM
I am not taking a result from server. i have create a while inside my server for not giving an aswer at all.
Tuesday, April 14, 2020 10:12 AM
Do you mean you create a server which won't return values? Does it only hang on the request? I think it is much more like a server part issue as it worked fine on my side. Can you test it with another rest API?
Tuesday, April 14, 2020 10:15 AM
If android trying to find a server, but server doesnt respond. Then it should work cancelationToken and timeout right?
Tuesday, April 14, 2020 10:24 AM
Thank you LandLu, this is what i will going to do, thank you so much for your help!
Thursday, May 14, 2020 5:05 PM
Hi Chris, did you get an answer on this, as I'm having the same problem. I'm using Xamarin forms 4.6.0.726, with vs 2019. I thought out of the box, I'd get the AndroidClientHandler() with httpclient, but I'm not so sure. I've set the HttpClient implementation to be Android and SSL/TLS to be Default (native TLS 1.2+) in Android Options Advanced on my Android project. I think this was the default anyway.
If I create a Httpclient with the AndroidClientHandler it does take notice of the timeout and task cancel. new HttpClient(new AndroidClientHandler();
What going on ? Should I be setting the client handler manually.
Thursday, May 14, 2020 6:13 PM
@What going on ? Should I be setting the client handler manually.
You will propably have the same result. I didn't found any solution yet. Till now i making export my projects in vs2017