无法返回json数据,WCF Resful Service .NET 4.0 [英] unable to return json data, WCF Resful Service .NET 4.0

查看:415
本文介绍了无法返回json数据,WCF Resful Service .NET 4.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近用Entity Framework 4.0设置了一个WCF Resful服务
它完美地与XML一起工作,但是当我尝试以json格式返回时,我得到了

  HTTP / 1.1 504 Fiddler  - 接收失败
内容类型:text / html
连接:关闭
时间戳:01:11:06.453

ReadResponse()失败:服务器未返回此请求的响应。

任何想法?



提前



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

硬码ResponseFormat方式:

  [OperationContract] 
[ WebInvoke(Method =GET,
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
UriTemplate =Deal / {id})]
优惠XMLDealDetail字符串id);

动态设置ResponseFormat方式:

  [OperationContract] 
[WebInvoke(Method =GET,
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = deal / {id} / {format})]
优惠XMLDealDetail(字符串ID,字符串格式);

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;
}
//交易是在edmx文件中定义的实体类
交易dealprice = DealsServices.GetById(id);
回报交易;

}

我在哪里?

解决方案

通过使用服务跟踪查看器,实际上有很多方法可以解决问题。
此链接帮了我
我终于看到了这个问题,但是堆积的收益与问题。



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



我将为此启动一个新问题。


I recently set up a WCF Resful Service with Entity Framework 4.0 It works with XML perfectly, however when I try to return it in json format I got

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.

Any Ideas??

thanks in advance

Edit: The Code is quite normal, actually i tried two way of doing it, but no luck.

Hard code ResponseFormat Way:

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

Dynamically Set ResponseFormat Way:

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

            }

where am i missing??

解决方案

By using Service Trace Viewer, actually there a number of ways of getting the problem. This link helped me. I finally see the problem, however being stacked gain with the problem.

'The type 'xxx.DataEntity.AppView' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.'

I will start a new question for that.

这篇关于无法返回json数据,WCF Resful Service .NET 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆