1

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.

Community
  • 1
  • 1
Mikkel
  • 1,771
  • 11
  • 35
  • 59

1 Answers1

2

You most likely call this on UI thread. When your method calls into client.GetAsync, context is remembered so that part after async call might execute on the same context as the part before. This is done for your convenience - after returning from client.GetAsync you are back on UI thread and can safely work with user interface controls.

But in this case you are blocking user interface thread by calling:

task.GetAwaiter().GetResult();

So UI thread is blocked by this and after client.GetAsync has finished - it cannot return back to UI thread - hence you are deadlocked. That's if explain in simple terms.

To fix, just don't block UI thread (you don't need anyway):

RoomsList = await _getRoomsService.SearchForAvailableRooms(searchModel.From, searchModel.To, searchModel.Smoke, searchModel.Minibar, searchModel.RoomValue, searchModel.CustomerValue);
Evk
  • 98,527
  • 8
  • 141
  • 191
  • Have tried this, it tells me that await operator can only be used within async method, but funny thing that my method is an async method? – Mikkel Apr 13 '16 at 12:35
  • 1
    You need to mark method where you do this as async. Suppose you are doing this in button click handler, then just do "async void OnButtonClick(object sender, RoutedEventArgs e)" – Evk Apr 13 '16 at 12:36
  • Okay, I give it a try! hang on! – Mikkel Apr 13 '16 at 12:37
  • Thank you for your quick reply ;) now I finally got a respond from the api! you rockz! – Mikkel Apr 13 '16 at 12:43