Use of Async and Await
From Legacy code:
....
var client = new WebClient();
var content = client.DownloadString($"http://localhost:62323/api/stocks
/{Ticker.Text}");
var data = JsonConvert.DeserializeObject
<IEnumberable<StrockPrice>>(content);
Stocks.ItemSource = data;
//same thing we are going to achieve using httpclient
...
try
{
using(var client = new HttpClient())
{
var result = await client.
GetAsync($"http://localhost:2323/api/stocks
/{Ticker.Text}")
var content = await result.content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject
<IEnumberable<StockPrice>>(content);
Stocks.ItemSource = data
}
}
catch(Exception ex)
{
}
...
....
var client = new WebClient();
var content = client.DownloadString($"http://localhost:62323/api/stocks
/{Ticker.Text}");
var data = JsonConvert.DeserializeObject
<IEnumberable<StrockPrice>>(content);
Stocks.ItemSource = data;
...
converted using Async and Await://same thing we are going to achieve using httpclient
...
try
{
using(var client = new HttpClient())
{
var result = await client.
GetAsync($"http://localhost:2323/api/stocks
/{Ticker.Text}")
var content = await result.content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject
<IEnumberable<StockPrice>>(content);
Stocks.ItemSource = data
}
}
catch(Exception ex)
{
}
...
Comments
Post a Comment