I'm using https://www.timeanddate.com/ this API for holiday details.
When I debugge The HttpClient response messege is "The operation was canceled." '($exception).CancellationToken.WaitHandle' threw an exception of type 'System.ObjectDisposedException'
and response in POSTMAN is [ ] (response type : 200k).
controller
public async Task<IActionResult> Get(string country, int year)
{
List<Holidays> holidays = new List<Holidays>();
holidays = await _holidayService.GetHolidays(country, year);
return Ok(holidays);
}
service
public HolidayService(HttpClient client)
{
_client = client;
_client.Timeout = TimeSpan.FromMinutes(3);
}
public async Task<List<Holidays>> GetHolidays(string country, int year)
{
string url = string.Format($"/holidays?accesskey=ACCESSKEY&secretkey=SECRET&version=3&country=ro&year=2021&lang=en");
var result = new List<Holidays>();
try
{
using var response = await _client.GetAsync(url);
}
// Filter by InnerException.
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
// Handle timeout.
Console.WriteLine("Timed out: " ex.Message);
}
catch (TaskCanceledException ex)
{
// Handle cancellation.
Console.WriteLine("Canceled: " ex.Message);
}
return result;
}
startup
HttpClientsConfig.InjectServices(services, Configuration);
services.AddHttpClient<IHolidayService, HolidayService>(c =>
{
c.BaseAddress = new System.Uri(Configuration.GetValue<string>("Holiday:Url"));
c.DefaultRequestHeaders.Add("Accept", "application/json");
});
AppSetting.development.json
"Holiday": {
"AccessKey": "MY_ACCESS_KEY",
"SecretKey": "MY_SECRET_KEY",
"Url": "https://api.xmltime.com"
}
HOW CAN I GET ALL RESPONSE WHICH IS LIKE THIS https://api.xmltime.com/holidays?accesskey={MY_ACCESS_KEY}&secretkey={MY_SECRET_KEY}&version=3&country=ro&year=2021&lang=en
I was trying to get API response more than 4 days. Please help me.. Advanced Thanks.
CodePudding user response:
It sounds that the exception class, TaskCanceledException
needs to be fed a CancellationToken
. However, the operation that was canceled that line below
using var response = await _client.GetAsync(url);
has not CancellationToken
. Therefore, I think you can fix the problem by the following solution:
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken token = source.Token;
var response = await _client.GetAsync(url,token);
Another possible solution is that, remove using
keyword at below line:
using var response = await _client.GetAsync(url);
it sounds that using
disposes client object before its CancellationToken
to be used by Exception Handler and complaining that CancellationToken.WaitHandle
disposed of before be able to use it.
CodePudding user response:
You could try to deserialize the response like below
public async Task<List<Holidays>> GetHolidays(string country, int year)
{
string url = string.Format($"/holidays?accesskey=ACCESSKEY&secretkey=ACCESSKEY&version=3&country=ro&year=2021&lang=en");
var result = new List<Holidays>();
try
{
_client.Timeout= Timespan. FromMinutes(30);
using var response = await _client.GetAsync(url);
string jsonString= await response.Content.ReadAsStringAsync();
var root=JsonConvert.DeserializeObject<Root>(jsonString);
return root.holidays;
}
// Filter by InnerException.
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
// Handle timeout.
Console.WriteLine("Timed out: " ex.Message);
}
catch (TaskCanceledException ex)
{
// Handle cancellation.
Console.WriteLine("Canceled: " ex.Message);
}
return result;
}
Alternatively you can use the nuget package
dotnet add package TimeAndDate.Services
var country = "no";
var service = new HolidaysService('accessKey', 'secretKey');
var result = service.GetHolidaysForCountry(country, 2020);