我是相當新的WCF的Web API,我有一個基本的服務去,並試圖利用使用HttpResponseMessage作爲返回類型的全功率。我試圖返回一個列表,並得到以下我無法解決的錯誤。WCF的Web API 0.6 - 在HttpResponseMessage返回列表<T>拋出一個異常
這是一個非常基本的直接XML服務。
任何想法,將不勝感激。謝謝。
類型 'System.Net.Http.HttpResponseMessage`1 [System.Collections.Generic.List`1 [Entities.UploadedDocumentSegmentType]]' 無法序列。考慮使用 DataContractAttribute屬性標記它,並標記您想要序列化爲DataMemberAttribute屬性的所有成員 。如果 類型是一個集合,請考慮使用 CollectionDataContractAttribute標記它。有關其他支持的類型,請參閱Microsoft .NET Framework 文檔。
這裏是我的服務:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class DocumentService
{
[WebGet(UriTemplate = "/GetAllUploadableDocumentTypes")]
public HttpResponseMessage<List<UploadedDocumentSegmentType>> GetAllUploadableDocumentTypes()
{
UploadedDocumentManager udm = new UploadedDocumentManager();
return new HttpResponseMessage<List<UploadedDocumentSegmentType>>(udm.GetAllUploadableDocumentTypes());
}
}
類UploadedDocumentSegmentType的定義是這樣的:
[Serializable]
public class UploadedDocumentSegmentType
{
public UploadedDocumentSegmentType();
public int DocTracSchemaID { get; set; }
public int ID { get; set; }
public string Type { get; set; }
}
我想這太:
[Serializable]
[DataContract]
public class UploadedDocumentSegmentType
{
public UploadedDocumentSegmentType();
[DataMember]
public int DocTracSchemaID { get; set; }
[DataMember]
public int ID { get; set; }
[DataMember]
public string Type { get; set; }
}
UDPATE:我使用WCF REST服務應用程序Visual Studio模板到cr服務於此服務。我從頭開始嘗試,並將示例WebGET方法的返回類型更改爲WebResponseMessage,它會在那裏引發相同的錯誤。所以這不是我的代碼,這是一些配置的事情,對我來說我無法弄清楚..
所以看起來問題在於更深層次的地方,也許一些配置設置?它在HttpResponseMessage中返回的任何東西上爆炸。即使是這樣的事情也會產生相同的錯誤。我知道我失去了一些東西真的很基本的和愚蠢的...... '[WebGet(UriTemplate = 「/ GetTest2」)]' 公共HttpResponseMessage GetTest2(){ 返回 新HttpResponseMessage ( 「測試」,HttpStatusCode 。好); } –
Spacetop
2012-02-07 15:14:34
我不認爲WCF REST服務應用程序VS模板實際上與WCF Web Api兼容。默認情況下,WCF Web API返回JSON/XML,並且不使用DataContractSerializer。 – 2012-02-08 17:23:24
您能否指出我正確的設置方向?謝謝。我認爲這是模板是Web API,因爲它取消了SVC文件...這是我使用的:http://visualstudiogallery.msdn.microsoft。com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd – Spacetop 2012-02-10 16:02:30