I
issam1975
Guest
i have a rest api written in asp.net core and deployed on the server that do a relatively long processing (around 15 minutes) then after finishing it return an "ok" status string.
if i run it from a web browser it work without a problem like expected . it did the job and return "ok" when finished .
but when consumed from a WPF application using httpclient the task is executed but i never get the result back . it continue to run indefinitely even all the tasks are done (i have checked that) .
here my button click event to start the operation
try
{
(sender as Button).IsEnabled = false;
savedtime = DateTime.Now;
// this is used to display a timer on the screen so users know how much time it's taking
System.Windows.Threading.DispatcherTimer dispatcherTimer = new
System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += DispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
await Task.Run(async () =>
{
await callserverapi();
});
dispatcherTimer.Stop();
(sender as Button).IsEnabled = true;
await Task.Delay(10000);
this.Close();
and here the callserverapi function
private async Task<string> callserverapi()
{
var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(30);
HttpResponseMessage response = await httpClient.GetAsync("https://myremoteserver.com/Api1");
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsAsync<string>();
}
return result;
}
also please note that if i use this same code snippet with a remote api that don't take time it work without a problem .
any idea on how to fix this please ?
thanks
Continue reading...
if i run it from a web browser it work without a problem like expected . it did the job and return "ok" when finished .
but when consumed from a WPF application using httpclient the task is executed but i never get the result back . it continue to run indefinitely even all the tasks are done (i have checked that) .
here my button click event to start the operation
try
{
(sender as Button).IsEnabled = false;
savedtime = DateTime.Now;
// this is used to display a timer on the screen so users know how much time it's taking
System.Windows.Threading.DispatcherTimer dispatcherTimer = new
System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += DispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
await Task.Run(async () =>
{
await callserverapi();
});
dispatcherTimer.Stop();
(sender as Button).IsEnabled = true;
await Task.Delay(10000);
this.Close();
and here the callserverapi function
private async Task<string> callserverapi()
{
var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMinutes(30);
HttpResponseMessage response = await httpClient.GetAsync("https://myremoteserver.com/Api1");
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsAsync<string>();
}
return result;
}
also please note that if i use this same code snippet with a remote api that don't take time it work without a problem .
any idea on how to fix this please ?
thanks
Continue reading...