title: Xamarin Form HTTPClient Call crashing
body: <p>I have a project where I am using System.Net.Http.HttpClient.  I am trying to centralize all calls to my Web APIs so that I have common error handing etc.  I created the following class in my project.  </p>
  
  <pre><code>using ModernHttpClient;
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Linq;
  using System.Net.Http;
  using System.Net.Http.Headers;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace WebAPIHelper
  {
      class WebAPICaller
      {
          public async Task&lt;string&gt; CallWebService(string ps_URI)
          {
              HttpClient lobj_HTTPClient = null;
              HttpResponseMessage lobj_HTTPResponse = null;
              string ls_Response = "";
  
              //We assume the internet is available. 
              try
              {
                  //Get the Days of the Week
                  lobj_HTTPClient = new HttpClient(new NativeMessageHandler());
                  lobj_HTTPClient.BaseAddress = new Uri(App.APIPrefix);
                  lobj_HTTPClient.DefaultRequestHeaders.Accept.Clear();
                  lobj_HTTPClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
                  lobj_HTTPResponse = await lobj_HTTPClient.GetAsync(ps_URI);
  
                  if (!lobj_HTTPResponse.IsSuccessStatusCode)
                  {
                      Debug.WriteLine(lobj_HTTPResponse.ReasonPhrase);
                  }
                  else
                  {
                      ls_Response = await lobj_HTTPResponse.Content.ReadAsStringAsync();
                  }
              }
              catch (Exception ex)
              {
                  Debug.WriteLine(ex.Message);
              }
              finally
              {
                  if (lobj_HTTPClient != null)
                      lobj_HTTPClient.Dispose();
                  if (lobj_HTTPResponse != null)
                  {
                      lobj_HTTPResponse.Dispose();
                  }
              }
  
              return ls_Response;
  
          }
  
      }
  }
  </code></pre>
  
  <p>I call the function from an instance object I created in my ViewModel class for languages as follows:</p>
  
  <pre><code>using ModernHttpClient;
  using Newtonsoft.Json;
  using System;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.Diagnostics;
  using System.Net.Http;
  using System.Net.Http.Headers;
  using System.Runtime.CompilerServices;
  using System.Threading.Tasks;
  
  namespace WebAPIHelper
  {
      public class VM_Languages : INotifyPropertyChanged
      {
          /// &lt;summary&gt;
          /// A collection for CGSLanguage objects.
          /// &lt;/summary&gt;
          public ObservableCollection&lt;GBSLanguage_ForList&gt; Items_ForList { get; private set; }
          const string ic_LanguagesAPIUrl = @"/languages/true";
  
          /// &lt;summary&gt;
          /// Constructor for the Languages view model.
          /// &lt;/summary&gt;
          public VM_Languages()
          {
              this.Items_ForList = new ObservableCollection&lt;GBSLanguage_ForList&gt;();
          }
  
          /// &lt;summary&gt;
          /// Indicates of the view model data has been loaded
          /// &lt;/summary&gt;
          public bool IsDataLoaded
          {
              get;
              private set;
          }
  
          /// &lt;summary&gt;
          /// Creates and adds a the countries to the collection.
          /// &lt;/summary&gt;
          public async Task LoadData()
          {
              HttpClient lobj_HTTPClient = null;
              HttpResponseMessage lobj_HTTPResponse = null;
              string ls_Response = "";
  
              try
              {
                  IsDataLoaded = false;
                  string ls_WorkLanguageURI = ic_LanguagesAPIUrl;
  
                  //Get the Days of the Week
                  lobj_HTTPClient = new HttpClient(new NativeMessageHandler());
                  lobj_HTTPClient.BaseAddress = new Uri(App.APIPrefix);
                  lobj_HTTPClient.DefaultRequestHeaders.Accept.Clear();
                  lobj_HTTPClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
                  **//This call will not work
                  //WebAPICaller lobj_APICaller = new WebAPICaller();
                  //ls_Response = lobj_APICaller.CallWebService(ls_WorkLanguageURI).Result;
  
                  //This call will work
                  lobj_HTTPResponse = await lobj_HTTPClient.GetAsync(ls_WorkLanguageURI);**
  
  
                  if (lobj_HTTPResponse.IsSuccessStatusCode)
                  {
  
                      if (this.Items_ForList != null)
                          this.Items_ForList.Clear();
  
                      ls_Response = await lobj_HTTPResponse.Content.ReadAsStringAsync();
                      Items_ForList = JsonConvert.DeserializeObject&lt;ObservableCollection&lt;GBSLanguage_ForList&gt;&gt;(ls_Response);
  
                  }
              }
              catch (Exception ex)
              {
                  Debug.WriteLine(ex.Message);
              }
              finally
              {
                  this.IsDataLoaded = true;
                  NotifyPropertyChanged("GBSLanguages_ForList");
              }
          }
  
          /// &lt;summary&gt;
          /// Notifies subscribers that a property has changed. 
          /// &lt;/summary&gt;
          public event PropertyChangedEventHandler PropertyChanged;
          private void NotifyPropertyChanged(String propertyName)
          {
              PropertyChangedEventHandler handler = PropertyChanged;
              if (null != handler)
              {
                  handler(this, new PropertyChangedEventArgs(propertyName));
              }
          }
  
          protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
          {
              PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p>I have all my ViewModels in a static class so I only ever get one instance of them as follows:</p>
  
  <pre><code>namespace WebAPIHelper
  {
      public static class ViewModelObjects
      {
          private static VM_Languages iobj_Languages;
  
          public static VM_Languages Languages
          {
              get
              {
                  if (iobj_Languages == null)
                      iobj_Languages = new VM_Languages();
                  return iobj_Languages;
              }
  
          }
      }
  }
  </code></pre>
  
  <p>In my on appearing code of my main page I have the following call to retrieve the data from the WebAPI</p>
  
  <pre><code>    protected override async void OnAppearing()
      {
          Device.BeginInvokeOnMainThread(() =&gt;
          {
              if (!ViewModelObjects.Languages.IsDataLoaded)
                  ViewModelObjects.Languages.LoadData();
  
          });
  
          //If the DOW and Language data are not loaded yet - wait
          while (!ViewModelObjects.Languages.IsDataLoaded)
          {
              await Task.Delay(1000);
          }
  
      }
  </code></pre>
  
  <p>The problem is when I attempt to use my WebAPICaller class, it appears to crash on the line.  I never get a return from it.  No exceptions are ever raised and my program never continues.</p>
  
  <pre><code>lobj_HTTPResponse = await lobj_HTTPClient.GetAsync(ps_URI);
  </code></pre>
  
  <p>If I make what I believe to be the exact same call from my ViewModel, it works.  (I have both the call to the WebAPICaller class as well as a direct call to GetAsync in the View Model so you can test it out by commenting and uncommenting.)</p>
  
  <p>Any idea as to what I am doing wrong? </p>
  
  <p>Link to full sample project:
  <a href="https://1drv.ms/u/s!Ams6cZUzaeQy3M8uGAuaGggMt0Fi-A" rel="nofollow noreferrer">https://1drv.ms/u/s!Ams6cZUzaeQy3M8uGAuaGggMt0Fi-A</a></p>