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.