【发布时间】:2011-05-18 07:01:50
【问题描述】:
最近我用 EF4 设置了 WCF 宁静服务。 当返回 XML 格式响应时,一切都解决了。但是,当涉及到 JSON 时,我得到了 504 错误。 unable to return json data, WCF Resful Service .NET 4.0
通过使用服务跟踪查看器进行更深入的挖掘: 我发现了这个错误:
'类型'xxx.DataEntity.AppView' 无法序列化为 JSON,因为 其 IsReference 设置为“真”。这 JSON格式不支持 参考因为没有 表示的标准化格式 参考。要启用序列化, 禁用 IsReference 设置 类型或适当的父类 类型。'
“AppView”是由 EF4 从存储过程生成的复杂对象类。 我花了很多时间谷歌如何禁用 IsReference,到目前为止结果很少。
有人吗?有什么解决办法吗?
提前致谢
代码:
[OperationContract]
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "App/{id}/{format}")]
AppView FuncDetail(string id, string format);
public AppView FuncDetail(string id, string format)
{
SetResponseFormat(format);
return AppSvcs.GetById(id);
}
private void SetResponseFormat(string format)
{
if (format.ToLower() == "json")
{
ResponseContext.Format = WebMessageFormat.Json;
}
else
{
ResponseContext.Format = WebMessageFormat.Xml;
}
}
【问题讨论】:
标签: json entity-framework serialization wcf-rest