title: TargetInvocationException when retrieving list from SOAP web service in Azure
body: <p>I have a SOAP web service which I am pulling various information from. Most functions are working correctly, but I need some functions to return a <code>List</code>.</p>
  
  <p>The <code>WebMethod</code> is defined like:</p>
  
  <pre><code>List&lt;MyType&gt; myTypes = new List&lt;MyTypes&gt;();
  
  [WebMethod]
  public List&lt;MyType&gt; GetAllMyTypes()
  {
      string sql = "SELECT * FROM MyType";
      DataTable dt = new DataTable();
      dt = Globals.GLS_DataQuery(sql);
  
      List&lt;MyType&gt; myType = new List&lt;MyType&gt;();
      foreach (DataRow row in dt.Rows)
      {
          MyType myType = new MyType()
          {
              ID = (int)row["Id"]
          };
  
          myTypes.Add(myType);
      }
  
      return myTypes;
  }
  </code></pre>
  
  <p>The web service is referenced in the main project and is called by:</p>
  
  <pre><code>client.GetAllMyTypesCompleted += client_GetAllMyTypesCompleted;
  client.GetAllMyTypesAsync();
  </code></pre>
  
  <p><code>client_GetAllMyTypesCompleted</code> is defined as:</p>
  
  <pre><code>private void client_GetAllMyTypesCompleted(object sender, GetAllMyTypesCompletedEventArgs e)
  {
      var collection = e.Result;
  }
  </code></pre>
  
  <p>It is here that the <code>TargetInvocationException</code> is thrown, specifically regarding <code>Result</code>. If you run the web service by itself the correct data is returned. For reference <code>GLS_DataQuery</code> is defined as:</p>
  
  <pre><code>public static DataTable GLS_DataQuery(string sql)
  {
      DataTable dt = new DataTable
      SqlCommand command = new SqlCommand(sql, connection);
      SqlDataAdapter adapter = new SqlDataAdapter(command);
      adapter.Fill(dt);
  
      return dt;
  }
  </code></pre>
  
  <p>So why am I seeing this error? Or how should I be returning a list of objects in this instance?</p>
  
  <p>It might be of relevance that the web service is hosted in Azure.</p>
  
  <p>EDIT: Attaching the debugger to the instance of the web service running in Azure sees that it does return the correct data. The error is thrown in a Xamarin phone app that is calling the web service. The "Message" of the error is simply a null reference error and the stack trace is:</p>
  
  <blockquote>
    <p>at
    MyApp.MyService.Service1SoapClient.EndGetAllMyTypes(IAsyncResult
    result)    at
    MyApp.MyService.Service1SoapClient.OnEndGetAllMyTypes(IAsyncResult
    result)    at
    System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult
    result)</p>
  </blockquote>