简体   繁体   English

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

[英]The remote server returned an unexpected response: (502) Bad Gateway in Azure WCF

I've uploaded a service with the following contract. 我已通过以下合同上传了一项服务。

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

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: 但是,当我检索元素时,出现错误:

The remote server returned an unexpected response: (502) Bad Gateway. 远程服务器返回了意外的响应:(502)错误的网关。

The weird part is that when the list is empty (no elements in the DB), the operation goes well. 奇怪的是,当列表为空(DB中没有元素)时,操作进行得很好。 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 Thing , it works also! 更奇怪的是,对数据库的操作在服务器上内部进行,如果我聚合元素的名称,然后将它们作为字符串发送出去或打包为Thing类的字符串属性,它也可以工作!

This is what it looks like when it doesn't work: 这是当它不起作用时的样子:

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

This is what it looks like when it does work: 这是它工作时的样子:

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;
}

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. 我需要逐个执行此操作,而不是使用LINQ查询。 Very confused... 很迷茫...

I think it might be related to serialization. 我认为这可能与序列化有关。

context.Things.ToList()

This code looks like you are sending the EntityObject back from the service. 这段代码看起来像是您从服务发送回EntityObject。 What about trying using a POCO instead of using "Thing" interface? 尝试使用POCO代替“ Thing”界面怎么办? Or, you can cast/transform the object and return it? 或者,您可以强制转换/转换对象并返回它?

context.Things.ToList()ToAnotherPlainClass()

Does your "Thing" class has only two properties? 您的“事物”类是否只有两个属性? Are you able to return like that without looping? 您能够像这样返回而不会循环吗?

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;
}

Have you checked this one on MSDN ? 您是否已在MSDN上检查过此项目 I haven't tested it yet but it seems like you need to apply [ApplyDataContractResolverAttribute] . 我尚未测试过,但似乎您需要应用[ApplyDataContractResolverAttribute]

In most of my projects, I used to create the response model which is different from the actual entity so I didn't have the problem that you are having now. 在我的大多数项目中,我曾经创建与实际实体不同的响应模型,所以我没有遇到现在遇到的问题。 If you haven't tried ApplyDataContractResolverAttribute , I would say you should try that. 如果您尚未尝试ApplyDataContractResolverAttribute ,那么我想您应该尝试一下。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WCF远程服务器返回了意外的响应:(400)错误的请求 - WCF The Remote Server Returned an Unexpected Response: (400) Bad Request WCF服务:远程服务器返回了意外的响应:(400)错误的请求 - WCF Service: The remote server returned an unexpected response: (400) Bad Request 远程服务器返回意外响应:(400)错误请求,WCF - The remote server returned an unexpected response: (400) Bad Request, WCF 远程服务器返回错误:(502) Bad Gateway。 在MVC中 - The remote server returned an error: (502) Bad Gateway. in MVC WCF 到 WCF。 远程服务器返回意外响应:(400) Bad Request - WCF to WCF. The remote server returned an unexpected response: (400) Bad Request WCF不允许大量列表。 引发“远程服务器返回了意外的响应:(400)错误的请求。”异常 - WCF not allowing large list . Throwing “The remote server returned an unexpected response: (400) Bad Request.” exception Wcf System.ServiceModel.ProtocolException:&#39;远程服务器返回了意外的响应:(400)错误的请求。 - Wcf System.ServiceModel.ProtocolException: 'The remote server returned an unexpected response: (400) Bad Request.' (另一个)远程服务器返回了意外的响应:(400)错误的请求-Castle Windsor,WCF - (Another) The remote server returned an unexpected response: (400) Bad Request - Castle Windsor, WCF wcf上传流-远程服务器返回了意外响应:(400)错误的请求 - wcf upload stream - The remote server returned an unexpected response: (400) Bad Request 调用Web服务返回远程服务器返回错误:(502)错误的网关 - Calling web service returns The remote server returned an error: (502) Bad Gateway
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM