title: The remote server returned an unexpected response: (502) Bad Gateway in Azure WCF
body: <p>I've uploaded a service with the following contract.</p>
  
  <pre><code>[ServiceContract]
  public interface Service
  {
    [OperationContract]
    void CreateThing(Thing thing);
    [OperationContract]
    List&lt;Thing&gt; RetrieveThings();
  }
  </code></pre>
  
  <p>When I call the creation method, everything works great, the instance is created in the DB and I get no errors. However, when I retrieve the elements, I get the error:</p>
  
  <blockquote>
    <p>The remote server returned an unexpected response: (502) Bad Gateway.</p>
  </blockquote>
  
  <p>The weird part is that when the list is empty (no elements in the DB), the operation goes well. Even more weird is that the operation against the DB works internally on the server and if I aggregate the names of the elements and send them out as a string or packaged in a string property of the class <em>Thing</em>, it works also!</p>
  
  <p>This is what it looks like when it doesn't work:</p>
  
  <pre><code>public List&lt;Thing&gt; RetrieveThings()
  {
    List&lt;Thing&gt; output;
    using (Context context = new Context())
      output = context.Things.ToList();
    return output;
  }
  </code></pre>
  
  <p>This is what it looks like when it does work:</p>
  
  <pre><code>public List&lt;Thing&gt; RetrieveThings()
  {
    List&lt;Thing&gt; output = new List&lt;Thing&gt;();
    using (Context context = new Context())
      foreach (Thing thing in context.Things.ToList())
        output.Add(new Thing {Id = thing.Id, Name = thing.Name});
    return output;
  }
  </code></pre>
  
  <p>What is it I'm doing differently here (under the hood and not consciously)? Apparently I have access to the DB and I have the privileges. I can read off all the information, too. Still, I have to create copies of my objects for some reason. And I need to do so each-by-each and not with a LINQ query. Very confused...</p>