Garry Marsland
Garry Marsland

Reputation: 1221

WCF Web API serialization issue

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

Upvotes: 0

Views: 3163

Answers (2)

Radenko Zec
Radenko Zec

Reputation: 7669

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

Upvotes: 1

chandmk
chandmk

Reputation: 3481

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.

Upvotes: 2

Related Questions