user2991548
user2991548

Reputation: 21

List data member in WCF service

I have written WCF service which has List<String> as data member. I am adding data to the list in my service.svc but I am not finding any data in my list. Here is my data contract class.

[datacontract]
public class CompositeType
    {
        //here are data members
        public List<String> strlist ;
        [DataMember]
        public List<String> StrList
        {
            get { return strlist; }
            set { strlist = value; }

        }
    }

in my service.svc

public void myservice(CompositeType com)
{
 comtype.StrList.add("abc");
 System.IO.File.WriteAllText(@"E:\\for1.txt", comType.StrList[0]);
}

But no data is found in that file. Please help me. I searched a lot but could not trace what's wrong in that list member.

And I used this service in my other app.

Upvotes: 2

Views: 3902

Answers (1)

flayn
flayn

Reputation: 5322

You have to mark the backing field as DataMember and not the property:

[DataContract]
public class CompositeType
    {
        [DataMember]
        public List<String> strlist ;

        public List<String> StrList
        {
            get { return strlist; }
            set { strlist = value; }

        }
    }

// With list

[DataContract]
public class CompositeType
    {
        [DataMember]
        public List<String> strlist = new List<String>();

        public List<String> StrList
        {
            get { return strlist; }
            set { strlist = value; }

        }
    }

Upvotes: 3

Related Questions