我正在使用https://www.timeanddate.com/這個API來獲取假期的細節。
當我除錯時,HttpClient的回應資訊是"該操作被取消了。"。 '($exception).CancellationToken.WaitHandle'拋出了一個'System.ObjectDisposedException'型別的例外
。而POSTMAN中的回應是[ ](回應型別: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)。
服務
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"/span>) 。
var result = new List< Holidays>();
try
{
using var response = await _client.GetAsync(url)。
}
///通過InnerException過濾。
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
//處理超時.
Console.WriteLine("Timeed out: " ex.Message)。
}
catch (TaskCanceledException ex)
{
//處理取消。
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"
}
我怎樣才能得到像這樣的所有回復https://api. xmltime.com/holidays?accesskey={MY_ACCESS_KEY}&secretkey={MY_SECRET_KEY}&version=3&country=ro&year=2021&lang=en
我試圖獲得超過4天的API回應。 請幫助我。 先進謝謝。
uj5u.com熱心網友回復:
聽起來例外類,TaskCanceledException需要被輸入一個CancellationToken。然而,下面那行被取消的操作是
using var response = await _client.GetAsync(url);
沒有CancellationToken。因此,我認為你可以通過以下解決方案來解決這個問題:
CancellationTokenSource source = new CancellationTokenSource()。
CancellationToken token = source.Token。
var response = await _client.GetAsync(url,token)。
另一個可能的解決方案是,洗掉下面一行的using關鍵詞:
using var response = await _client.GetAsync(url);
聽起來,使用處置客戶端物件之前,它的CancellationToken被例外處理程式使用,并抱怨CancellationToken.WaitHandle在能夠使用它之前被處置了。
uj5u.com熱心網友回復:
你可以嘗試像下面這樣對回應進行反序列化
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"/span>) 。
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;
}
///通過InnerException過濾。
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
//處理超時.
Console.WriteLine("Timeed out: " ex.Message)。
}
catch (TaskCanceledException ex)
{
//處理取消。
Console.WriteLine(" Canceled: " ex.Message);
}
return result;
}
另外,你可以使用nuget包
。dotnet add package TimeAndDate.Services
var country = "no"/span>;
var service = new HolidaysService('accessKey'/span>, 'secretKey'/span>);
var result = service.GetHolidaysForCountry(country, 2020)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/328671.html
標籤:
下一篇:在Ktor中驗證請求
