I'm using one httpclient instance to send multiple requests to rest web api to get data. Here is what my code looks like:
First I have a control layer that calls the data layer for data.
public class ControlLayer
{
protected DataLayer dal;
//constructors here
public int getInfo1(int param)
{
int ret = this.dal.getInfo1(param);
return ret;
}
public int getInfo2(int param)
{
int ret = this.dal.getInfo2(param);
return ret;
}
}
then I have the dataLayer that calls webAPI, it looks like this. here for simplicity, im using .result directly.
public class DataLayer
{
HttpClient client = new HttpClient();
string url = "";
public int getInfo1(int param1)
{
int ret=0;
HttpResponseMessage response = client.GetAsync(url).Result;
//.... do some work, get the value for ret
return ret;
}
public int getInfo2(int param1)
{
int ret = 0;
HttpResponseMessage response = client.GetAsync(url).Result;
//.... do some work, get the value for ret
return ret;
}
}
my questions is I've seen some tutorials saying that we should avoid using .result, because it might lead to deadlock. I'm not sure in my case do I need to use async/await? if I do need, I know I should async all the way down, but I do want my controlLayer to be sync, because I have other layer that calls the controlLayer's function, I don't want all the layer's function to be async and the result be Task<>, is this a situation of sync over async? am I miss something? any suggestions are appreciated. thanks!