2012-02-06 69 views
0

我是相當新的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,它會在那裏引發相同的錯誤。所以這不是我的代碼,這是一些配置的事情,對我來說我無法弄清楚..

+0

所以看起來問題在於更深層次的地方,也許一些配置設置?它在HttpResponseMessage中返回的任何東西上爆炸。即使是這樣的事情也會產生相同的錯誤。我知道我失去了一些東西真的很基本的和愚蠢的...... '[WebGet(UriTemplate = 「/ GetTest2」)]' 公共HttpResponseMessage GetTest2(){ 返回 新HttpResponseMessage ( 「測試」,HttpStatusCode 。好); } – Spacetop 2012-02-07 15:14:34

+0

我不認爲WCF REST服務應用程序VS模板實際上與WCF Web Api兼容。默認情況下,WCF Web API返回JSON/XML,並且不使用DataContractSerializer。 – 2012-02-08 17:23:24

+0

您能否指出我正確的設置方向?謝謝。我認爲這是模板是Web API,因爲它取消了SVC文件...這是我使用的:http://visualstudiogallery.msdn.microsoft。com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd – Spacetop 2012-02-10 16:02:30

回答

0

爲什麼你需要返回一個列表<T>?只要返回一個數組,它應該沒問題。我不認爲內置JSON串行器知道如何處理列表<T>。一個合理的事情,不知道如何做列表<T>是「可編輯的」通過添加/刪除類型的調用。

此外,您不需要wcf-web-api的datacontract/member屬性。

+0

謝謝斯科特,我想我不需要裝飾品。我已經看到它在其他項目中使用List ,但它們可能使用了不同的JSON序列化程序。我實際上使用內置的XML序列化器。我會嘗試一個數組,因爲你建議,但它似乎是一個解決方案,因爲我查詢的DLL吐出一個列表和客戶端需要一個列表與.. – Spacetop 2012-02-07 14:43:07

+0

我只是試圖投入到一個數組並返回,我得到了與上面列表相同的確切錯誤。它似乎有對象本身的問題.. – Spacetop 2012-02-07 14:55:17

+0

我的意思是說「安東尼」,對不起:) – Spacetop 2012-02-07 16:01:41