WCF Web API 0.6-返回列表< T>在HttpResponseMessage中引发异常 [英] WCF Web API 0.6 - Returning List<T> in HttpResponseMessage throws an exception

查看:115
本文介绍了WCF Web API 0.6-返回列表< T>在HttpResponseMessage中引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对WCF Web API还是很陌生,我拥有一项基本服务,正在尝试利用 HttpResponseMessage 作为返回类型的全部功能.我正在尝试返回一个列表,并遇到以下我无法解决的错误.

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.

这是一个非常基本的直截了当的XML服务.

This is a very basic straight-up XML service.

任何想法都将不胜感激.谢谢.

Any ideas would be appreciated. Thanks.

类型 'System.Net.Http.HttpResponseMessage`1 [System.Collections.Generic.List`1 [Entities.UploadedDocumentSegmentType]]' 无法序列化.考虑使用 DataContractAttribute属性,并将其标记为您的所有成员 要使用DataMemberAttribute属性序列化.如果类型 是一个集合,请考虑将其标记为 CollectionDataContractAttribute.请参阅Microsoft .NET Framework 其他受支持类型的文档.

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.

这是我的服务

[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类的定义如下:

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; }
}

我也尝试过:

[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,它将在此处引发相同的错误.因此,这不是我的代码,而是一些配置问题,我一生都无法理解..

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..

推荐答案

为什么需要返回List< T> ;?只需返回一个数组就可以了.我不认为内置的JSON-Serializer知道如何处理List< T>.不知道如何做为List< T>是合理的事情.通过添加/删除类型调用是可编辑的".

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.

此外,您不需要使用wcf-web-api的datacontract/member属性.

Also, you don't need the datacontract/member attributes with wcf-web-api.

这篇关于WCF Web API 0.6-返回列表&lt; T&gt;在HttpResponseMessage中引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆