I'm sending a request from a service and explicitly calling the logout API to free resources but I'm encountering the error in the title. I'm using Task.Run() to prevent deadlocks.
Currently this is my code:
var baseAddress = new Uri(baseServiceUrl);
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
{
client.Timeout = TimeSpan.FromSeconds(3600);
client.DefaultRequestHeaders.Authorization = authentication;
var requestUrl = GenerateServiceRequestUrl();
using (var response = Task.Run(() => client.GetAsync(requestUrl)).GetAwaiter().GetResult())
{
//call logout explicity
using (Task.Run(() => client.GetAsync(baseServiceUrl + LogoutUrl).GetAwaiter().GetResult())) { }
return new MyServiceResponse(response, getContent);
}
}
The error is from this line of code calling the logout explicitly:
using (Task.Run(() => client.GetAsync(_baseUrl + LogoutUrl).GetAwaiter().GetResult())) { }
However when I try to change it to the code below, the error is gone:
using (client.GetAsync(baseServiceUrl + LogoutUrl).Result) { }
My question is that is there a way to use Task.Run() for the logout call?