Hari Gillala
Hari Gillala

Reputation: 11916

How to use [DataMember] for IEnumerable<string> for property in WCF

I am trying to use [DataContract] and [DataMember] for the below class.

[DataContract]
public abstract class TestClass
{
    #region Public Contructors        
    protected TestClass()
    {
    }
    #endregion

    #region Public Properties   

    [DataMember]
    public IEnumerable<string> Subjects { get; set; }

    #endregion      


}

I get the following error

Cannot serialize member TestClass.Subjects of type System.Collections.Generic.IEnumerable`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] because it is an interface

What should I to fix this issue please?

Upvotes: 0

Views: 3775

Answers (1)

Agustin Meriles
Agustin Meriles

Reputation: 4854

As the error message says, you can't serialize an Interface like IEnumerable. Try using a concrete collection like List<string> or even string[].

Keep in mind that the serialized classes that you send over WCF will probably better to be surrogate classes or DTO, so this way you don't mess up your Business Model.

Hope this helps

Upvotes: 2

Related Questions