代码之家  ›  专栏  ›  技术社区  ›  D.J

无法返回JSON数据,wcf resful service.net 4.0

  •  2
  • D.J  · 技术社区  · 15 年前

    我最近用Entity Framework 4.0建立了一个wcf resful服务 它与XML完美结合,但是当我尝试以JSON格式返回它时,我得到了

    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.
    

    有什么想法吗??

    提前谢谢

    编辑: 代码很正常,实际上我尝试了两种方法,但没有运气。

    硬编码应答方式:

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

    动态设置响应格式:

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

    我在哪里失踪??

    2 回复  |  直到 13 年前
        1
  •  4
  •   Paul Fleming    13 年前

    通过使用 服务跟踪查看器 实际上,有很多方法可以解决这个问题。 This link 帮助了我。 我终于看到了问题所在,但却被问题堆积起来了。

    “类型”xxx.dataEntity.appView“无法序列化到JSON,因为其IsReference设置为”true“。”JSON格式不支持引用,因为没有用于表示引用的标准格式。若要启用序列化,请对类型或该类型的适当父类禁用IsReference设置。

    我将为此提出一个新问题。

        2
  •  1
  •   Paul Fleming    13 年前

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

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

    如果参数1或 param2 为空或MinValue(对于 DateTime )所以我有问题。当我设置所有参数时,问题就消失了。