WCF Web API 0.6 - 返回列表在 HttpResponseMessage 中抛出异常
我对 WCF Web API 相当陌生,我有一个基本服务,并尝试充分利用使用 HttpResponseMessage 作为返回类型的全部功能。我试图返回一个列表并收到以下我无法解决的错误。
这是一个非常基本的直接 XML 服务。
任何想法将不胜感激。谢谢。
类型 'System.Net.Http.HttpResponseMessage`1[System.Collections.Generic.List`1[Entities.UploadedDocumentSegmentType]]' 无法序列化。考虑用 DataContractAttribute 属性,并标记其所有成员 希望使用 DataMemberAttribute 属性进行序列化。如果类型 是一个集合,考虑用 集合数据契约属性。请参阅 Microsoft .NET 框架 其他支持类型的文档。
这是我的服务:
[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 模板来创建服务。我尝试从头开始将示例 WebGET 方法的返回类型更改为 WebResponseMessage,但它会抛出相同的错误。所以这不是我的代码,而是一些我一生都无法弄清楚的配置。
I'm fairly new to the WCF Web API, I have a basic service going and trying to leverage the full power of using HttpResponseMessage as return type. I am trying to return a List and getting the following error around which I can't get around.
This is a very basic straight-up XML service.
Any ideas would be appreciated. Thanks.
Type
'System.Net.Http.HttpResponseMessage`1[System.Collections.Generic.List`1[Entities.UploadedDocumentSegmentType]]'
cannot be serialized. Consider marking it with the
DataContractAttribute attribute, and marking all of its members you
want serialized with the DataMemberAttribute attribute. If the type
is a collection, consider marking it with the
CollectionDataContractAttribute. See the Microsoft .NET Framework
documentation for other supported types.
Here's my service:
[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());
}
}
The class UploadedDocumentSegmentType is defined as such:
[Serializable]
public class UploadedDocumentSegmentType
{
public UploadedDocumentSegmentType();
public int DocTracSchemaID { get; set; }
public int ID { get; set; }
public string Type { get; set; }
}
And I tried this too:
[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: I used WCF REST Service Application Visual Studio template to create the service. I tried from scratch and change the return type on the sample WebGET method to WebResponseMessage and it would throw the same error there. So it's not my code, it's some configuration thing which for the life of me I can't figure out..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么需要返回一个List?只要返回一个数组就可以了。我认为内置的 JSON-Serializer 不知道如何处理 List。不知道如何做 List是合理的事情。通过添加/删除类型调用“可编辑”。
此外,您不需要 wcf-web-api 的数据契约/成员属性。
Why do you need to return a List<T>? Just return an array and it should be okay. I don't think the built-in JSON-Serializer knows how to handle a List<T>. A reasonable thing to not know how to do as a List<T> is "editable" via add/remove type calls.
Also, you don't need the datacontract/member attributes with wcf-web-api.