我最近使用 Entity Framework 4.0 设置了 WCF Restful 服务
它与 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.
有任何想法吗??
提前致谢
Edit:代码很正常,实际上我尝试了两种方法,但没有运气。
硬编码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;
}
我在哪里失踪了?
通过使用服务跟踪查看器,实际上有很多方法可以解决这个问题。这个链接 http://professionalaspnet.com/archive/2010/09/02/Turn-on-WCF-Tracing-for-WCF-Troubleshooting.aspx帮助过我。
我终于看到了问题所在,但问题却让我获益匪浅。
'类型'xxx.DataEntity.AppView'无法序列化为JSON,因为其IsReference设置为'True'。 JSON 格式不支持引用,因为没有用于表示引用的标准化格式。要启用序列化,请禁用该类型或该类型的适当父类上的 IsReference 设置。
我将为此提出一个新问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)