远程服务器返回意外响应:(502)Azure WCF中的错误网关

时间:2016-02-17 01:08:14

标签: c# entity-framework linq wcf azure

我已使用以下合同上传了服务。

[ServiceContract]
public interface Service
{
  [OperationContract]
  void CreateThing(Thing thing);
  [OperationContract]
  List<Thing> RetrieveThings();
}

当我调用创建方法时,一切都很好,实例是在数据库中创建的,我没有错误。但是,当我检索元素时,我收到错误:

  

远程服务器返回意外响应:(502)Bad Gateway。

奇怪的是,当列表为空(DB中没有元素)时,操作进行得很顺利。更奇怪的是,针对数据库的操作在服务器上内部工作,如果我聚合元素的名称并将它们作为字符串发送出去或打包在类 Thing 的字符串属性中,它也有效!

这是它不起作用时的样子:

public List<Thing> RetrieveThings()
{
  List<Thing> output;
  using (Context context = new Context())
    output = context.Things.ToList();
  return output;
}

这就是它工作时的样子:

public List<Thing> RetrieveThings()
{
  List<Thing> output = new List<Thing>();
  using (Context context = new Context())
    foreach (Thing thing in context.Things.ToList())
      output.Add(new Thing {Id = thing.Id, Name = thing.Name});
  return output;
}

我在这里采取了哪些不同的做法(在引擎盖下而不是有意识地)?显然我可以访问数据库,我有权限。我也可以阅读所有信息。不过,由于某种原因,我必须创建我的对象的副本。而且我需要逐个进行,而不是使用LINQ查询。很困惑......

1 个答案:

答案 0 :(得分:1)

我认为它可能与序列化有关。

context.Things.ToList()

此代码看起来就像是从服务发回EntityObject。那么尝试使用POCO而不是使用“Thing”界面呢?或者,您可以转换/转换对象并返回它?

context.Things.ToList()ToAnotherPlainClass()

你的“Thing”类只有两个属性吗?您是否能够在没有循环的情况下返回?

public List<Thing> RetrieveThings()
{
  List<Thing> output;
  using (Context context = new Context())
    output = (from t in context.Things
               select new Thing() {
                  Id = t.Id, Name = t.Name
               }
             ).ToList();
  return output;
}

您检查了this one on MSDN吗?我还没有测试过,但似乎你需要申请[ApplyDataContractResolverAttribute]

在我的大多数项目中,我曾经创建过与实际实体不同的响应模型,所以我没有你现在遇到的问题。如果你还没有尝试ApplyDataContractResolverAttribute,我会说你应该尝试一下。