2010-11-08 56 views
1

我最近成立了一个WCF服务Resful实体框架4.0 它与XML完美的,但是当我尝试在JSON格式返回它我无法返回JSON数据,WCF Resful服务.NET 4.0

HTTP/1.1 504 Fiddler - Receive Failure 
Content-Type: text/html 
Connection: close 
Timestamp: 01:11:06.453 

ReadResponse() failed: The server did not return a response for this request. 

任何想法?

在此先感谢

编辑: 守则是很正常的,其实我想这样做的双向,但没有运气。

硬代码ResponseFormat方式:

[OperationContract] 
     [WebInvoke(Method = "GET", 
      BodyStyle = WebMessageBodyStyle.Wrapped, 
      ResponseFormat = WebMessageFormat.Json, 
      UriTemplate = "Deal/{id}")] 
     Deals XMLDealDetail(string id); 

动态设置ResponseFormat方式:

[OperationContract] 
      [WebInvoke(Method = "GET", 
       BodyStyle = WebMessageBodyStyle.Wrapped, 
       ResponseFormat = WebMessageFormat.Json, 
       UriTemplate = "Deal/{id}/{format}")] 
      Deals XMLDealDetail(string id, string format); 

    public Deals XMLDealDetail(string id, string format) 
      { 
       OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse; 

       if (format.ToLower() == "json") 
       { 
        context.Format = WebMessageFormat.Json; 
        context.ContentType = "application/json"; 
       } 
       else 
       { 
        context.Format = WebMessageFormat.Xml; 
       } 
//Deals is a Entity Class defined in edmx file 
        Deals deal = DealsServices.GetById(id); 
        return deal; 

      } 

我在哪里丢失的?

+0

也许显示你的代码会给我们更多的想法? – 2010-11-08 13:08:12

+0

+1同样的问题,你有它工作的DJ? – andy 2010-11-08 23:35:29

+0

添加代码,我猜这可能是一个设置问题,但有点失去了我应该看的方向 – 2010-11-09 02:36:37

回答

3

通过使用服务跟踪查看器,实际上有许多方法来解决问题。 This link帮助了我。 我终于看到了问题,但是被问题堆积了。

'类型'xxx.DataEntity.AppView'无法序列化为JSON,因为它的IsReference设置为'True'。 JSON格式不支持引用,因为没有用于表示引用的标准格式。要启用序列化,请禁用该类型的IsReference设置或该类型的适当父类。

我会为此开始一个新问题。

0

当我没有设置一个对象的参数时,我遇到了同样的问题。 比如我有一个类:

public class Obj { 
    int param1 { get; set; } 
    int param2 { get; set; } 
}; 

如果参数1或param2为空或minValue(最小值)(为DateTime),所以我得到了这个问题。当我设置所有参数时,问题就消失了。