tags:

views:

1373

answers:

2

I have a collection class that I have decorated with a CollectionDataContract. The collection class also has a property on the class that I would like to be passed to the service client. I have tried adding [DataMember] to that property but it didn't add it to the class in the client when I updated.

Any WCF experts out there have any help to offer?

+2  A: 

You might want to consider implementing custom serialization for your class. It might be easiest to implement IXmlSerializable, output the values of your custom property and then use the DataContractSerializer to serialize instances of the child items in the collection to the output.

casperOne
+3  A: 

A working solution is posted on my blog:

http://borismod.blogspot.com/2009/04/wcf-collectiondatacontract-and.html

UPD: Thanks, for your comment, Jeff. Here is a summary here of a non generic class. A full generic solution can be found in a new post in my blog: http://borismod.blogspot.com/2009/06/v2-wcf-collectiondatacontract-and.html


[DataContract(IsReference = true)]    
public class EntityCollectionWorkaround : ICollection 
    {

        #region Constructor
        public EntityCollectionWorkaround()
        {
            Entities = new List();
        } 
        #endregion

        [DataMember]
        public int AdditionalProperty { get; set; }

        [DataMember]
        public List Entities { get; set; }

        #region ICollection Members
          // Implement here members of ICollection by wrapping Entities methods
        #endregion

        #region IEnumerable Members
          // Implement here members of IIEnumerable by wrapping Entities methods
        #endregion

    }

Boris Modylevsky
Good blog post, but it would be nice if you gave a quick summary here.
Jeff Martin