最近我已經建立了一個WCF與EF4的寧靜服務。 當返回XML格式的響應時,這一切都奏效了。但是當談到JSON時,我得到了504錯誤。 unable to return json data, WCF Resful Service .NET 4.0WCF restful使用實體框架複合返回JSON
通過挖通過使用服務跟蹤查看更深: 我發現這個錯誤:
'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.'
的「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;
}
}