Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add a DataMember to a CollectionDataContract in WCF?

Tags:

.net

wcf

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?

like image 725
Jeff Martin Avatar asked Jan 23 '09 00:01

Jeff Martin


1 Answers

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

    }

like image 143
Boris Modylevsky Avatar answered Oct 14 '22 21:10

Boris Modylevsky