I have a problem when calling the api application I'm running in a different project. When I debug, I can see the call hits the right controller in the api. After I click continue, it just hangs and nothing happens.
Method where I make the call to the Api
public async Task<ObservableCollection<Room>> SearchForAvailableRooms(DateTime from, DateTime to, bool smoke, bool minibar, int rooms, int customers)
{
ObservableCollection<Room> data = new ObservableCollection<Room>();
// Call api to get rooms
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:15998/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// New code:
HttpResponseMessage response = await client.GetAsync("api/room");
if (response.IsSuccessStatusCode)
{
var jsonAsString = await response.Content.ReadAsStringAsync();
}
else
{
throw new ApplicationException(string.Format("Response message is not OK. Issues in action: {0}", "api/room"));
}
return null;
}
The client.GetAsync("api/room") makes the call to the api, but after it dosen't get any further.. The if or else are not being hitted.
Have read that the method call will causing a deadlock (hanging) from this stack post:Link to the post
He explain it has something to do where I make the call to the method. Right now I'm using these lines to call the method:
var task = _getRoomsService.SearchForAvailableRooms(searchModel.From, searchModel.To, searchModel.Smoke, searchModel.Minibar, searchModel.RoomValue, searchModel.CustomerValue);
var result = task.GetAwaiter().GetResult();
RoomsList = result;
Can any see what could be wrong here?
Update:
What I also have tried:
RoomsList = await _getRoomsService.SearchForAvailableRooms(searchModel.From, searchModel.To, searchModel.Smoke, searchModel.Minibar, searchModel.RoomValue, searchModel.CustomerValue);
This gives me an error --> Await operator can only be used within async method.