WCF Web API 序列化问题

发布于 2024-12-29 14:42:15 字数 2196 浏览 0 评论 0原文

我在使用 WCF Web API 0.6.0 返回 HttpResponseMessage 中的 ListIList 时遇到一些问题。

我的简单服务契约是:

[ServiceContract]
public interface IPersonService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "people", Method = "GET")]
    HttpResponseMessage<IList<Person>> LoadPeople();
}

实现是:

public class PersonService : IPersonService
{
    public HttpResponseMessage<IList<Person>> LoadPeople()
    {
        var people = new List<Person>();
        people.Add(new Person("Bob"));
        people.Add(new Person("Sally"));
        people.Add(new Person("John"));
        return new HttpResponseMessage<IList<Person>>(people);
    }
}

Person 类是这样的:

[DataContract]
public class Person
{
    public Person(string name)
    {
        Name = name;
    }

    [DataMember]
    public string Name { get; set; }
}

但是当我调用该方法时,出现以下异常:

<块引用>

System.Runtime.Serialization.InvalidDataContractException:类型“System.Net.Http.HttpResponseMessage1[System.Collections.Generic.IList1[Person]]”无法序列化。请考虑使用 DataContractAttribute 属性标记它,并使用 DataMemberAttribute 属性标记要序列化的所有成员。如果类型是集合,请考虑使用 CollectionDataContractAttribute 对其进行标记。有关其他支持的类型,请参阅 Microsoft .NET Framework 文档。

显然,序列化 IList 时存在问题。我的 Person 类已经指定了 DataContract 和 DataMember 属性,因此我仔细阅读后发现无法序列化接口。

我尝试将集合的类型从 IList 更改为 List,但仍然返回相同的错误。

我什至尝试创建一个 PersonCollection 类,并按照建议使用 CollectionDataContract 属性对其进行标记:

[CollectionDataContract]
public class PersonCollection : List<Person>
{
}

但这仍然不起作用,返回完全相同的错误。阅读更多内容,我发现 此错误被标记为已关闭(不会修复)。

任何人都可以帮忙,或者提供合适的替代方法吗?非常感谢。

更新

在遇到很多奇怪的问题后,我对代码进行了重大重构,问题似乎已经消失了。我现在返回一个包装 IList 的 HttpResponseMessage,它工作正常。

感谢您的所有帮助,但我确实相信我可能一直在查看 Heisenbug.. 。

I'm having some issues with returning either a List<T> or IList<T> within a HttpResponseMessage using WCF Web API 0.6.0.

My simple service contract is:

[ServiceContract]
public interface IPersonService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "people", Method = "GET")]
    HttpResponseMessage<IList<Person>> LoadPeople();
}

The implementation is:

public class PersonService : IPersonService
{
    public HttpResponseMessage<IList<Person>> LoadPeople()
    {
        var people = new List<Person>();
        people.Add(new Person("Bob"));
        people.Add(new Person("Sally"));
        people.Add(new Person("John"));
        return new HttpResponseMessage<IList<Person>>(people);
    }
}

And the Person class is such:

[DataContract]
public class Person
{
    public Person(string name)
    {
        Name = name;
    }

    [DataMember]
    public string Name { get; set; }
}

But when I invoke the method, I get the following exception:

System.Runtime.Serialization.InvalidDataContractException: Type 'System.Net.Http.HttpResponseMessage1[System.Collections.Generic.IList1[Person]]' 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.

So obviously there is an issue serializing an IList. My Person class already has the DataContract and DataMember attributes specified, so I read around a bit and discovered that you can't serialize an interface.

I tried changing the type of the collection from IList to List but the same error is still returned.

I've even tried creating a PersonCollection class and marked it with the CollectionDataContract attribute as recommended:

[CollectionDataContract]
public class PersonCollection : List<Person>
{
}

But this still doesn't work, with exactly the same error returned. Reading around some more I found this bug which is marked as closed (won't fix).

Anyone able to help please, or provide a suitable alternative method? Many thanks.

Update

After having lots of strange problems with this I refactored my code significantly and the issue seems to have disappeared. I'm now returning a HttpResponseMessage wrapping an IList and it works fine.

Thank you for all the help, but I do believe I may have been looking at a Heisenbug...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走过海棠暮 2025-01-05 14:42:15

不要在 wcf 方法中返回 IList。返回用 List 包装的 HttpResponseMessage 怎么样?

[编辑]

再看一下,问题不在于 IList,而在于 HttpResponseMessage 类。而且它是不可序列化的。

Do not return IList in the wcf method. How about returning HttpResponseMessage wrapped with List?

[Edit]

On a second look problem is not with IList, it is with HttpResponseMessage class. And it is not serializable.

诗酒趁年少 2025-01-05 14:42:15

我已经使用 IEnumerable 来完成相同的任务。它的作用就像魅力......

I have used IEnumerable for same task. It works like charm...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文