WCF CollectionDataContract
Asked Answered
I

3

7

I recently noticed in one of the articles the wcf service operation returned a collectiondatacontract

Users GetUsers(string someInput);

And Users type was defined as below:

[CollectionDataContract]
    public class Users : List<User>
    {
        public Users()
        {
        }

        public Users(IEnumerable<User> users) : base(users)
        {
        }
    }

Does returning a collectiondatacontract (like Users in this case) serve a different purpose than simply returning List<User> ?

Interrupt answered 21/4, 2011 at 0:34 Comment(2)
Thanks! I used this, but also needed to call the base constructor to make the Help page generation work - so in your example replace: 'public Users() {}' with 'public Users() : base() {}'.Forehanded
@IanGrainger Something else must have been amiss - the no-arg base constructor is called automatically if not otherwise specified..Steapsin
L
7

As far as I understand, this attribute will give you some control over what names the elements will have in the final xml string, after the DataContractSerializer has done its work serializing your collection.

This can be useful when you have to parse the result later manualy( in other words you will know what element to look for in that xml text, to find your collection and its parts).

Take a look at this for examples and more info:

http://msdn.microsoft.com/en-us/library/aa347850.aspx

Lashing answered 21/4, 2011 at 1:4 Comment(0)
I
1

if you return a List, the data serializer has a specific way of generating xml. I dont know how it does it for List, but if it was an array it would have generated something like -

<arrayOfUsers>
<User>
...here User object 1
</User>
...etc.
</arrayOfUsers>

But using CollectionDataContract you can serialize it and better expose it for consumers who may be creating XML by hand. Example - I would be able to give - CollectionDataCOntract(Name="AllUsers") // i dont remember ItemName or Name

then the XML expected would be something similar to -

<AllUsers>
<User>
...here User object 1
</User>
...etc.
</AllUsers>

Thats one utility for this.

Indication answered 21/4, 2011 at 0:51 Comment(0)
B
0

Just to expound on Andrei's answer and share my experience, I just went through an issue that I finally resolved using CollectionDataContract. Basically, in order to interface with a specific system, I wanted to be able to send and recieve xml of the format:

<SomeMessageList>
  <Message>
    <ID>blah</ID>
    <data1>blah</data1>
    <data2>etc.etc.</data2>
  </Message>
  <Message>
    <ID>blah</ID>
    <data1>blah</data1>
    <data2>etc.etc.</data2>
  </Message>
  //any number of repeated <Message> here
</SomeMessageList>

However, if I used an array or a List object, the root tag was always called ArrayOfMessage. And if I created a class that held an array of Message objects (lets say called MsgList), then WCF would add that as an extra tag in the mix, which I could not find a way to get rid of. So it would have looked like:

<SomeMessageList>
  <MsgList>
    <Message>
      <ID>blah</ID>
      <data1>blah</data1>
      <data2>etc.etc.</data2>
    </Message>
    //any number of repeated <Message> here
  </MsgList>
</SomeMessageList>

So CollectionDataContract just gave me a simple way to control the name of the root list element.

Bootie answered 22/3, 2012 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.