0

My Xamarin app deadlocks when trying to make API call (asp.net core web API). The mobile app is using Android emulator. I created both API and client app following Microsoft’s own tutorial. Requests run normally when making call with Postman or directly in the browser of my dev machine.

Constants class:

public static class Constants
{
        public static string BaseAddress =
        DeviceInfo.Platform == DevicePlatform.Android
          ? "https://10.0.2.2:44348"
          :"https://localhost:44348";

        public static string AppointmentsUrl = $"{BaseAddress}/api/appointments/";
}

Handler:

 public class AppointmentHandler
    {
        HttpClient client;
        JsonSerializerOptions serializerOptions;

        private List<Appointment> Appointments { get; set; }

        public AppointmentHandler()
        {
            client = new HttpClient();
            serializerOptions = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                WriteIndented = true
            };
        }


        public async Task<List<Appointment>> GetAppointments()
        {
            Appointments = new List<Appointment>();
            try
            {
                Uri uri = new Uri(string.Format(Constants.AppointmentsUrl, string.Empty));

                // the program deadlocks here
                HttpResponseMessage response = await this.client.GetAsync(uri);
                
                if (response.IsSuccessStatusCode)
                {
                    var content = await response.Content.ReadAsStringAsync();
                    return JsonSerializer.Deserialize<List<Appointment>>(content, serializerOptions);
                }

            }
            catch (Exception e)
            {
                var m = e.Message;
            }
            return null;
        }
Anonymous Asked question May 14, 2021