Collections

In .NET, a collection is any type that supports the IEnumerable or IEnumerable<T> interface. All of the built-in collections in .NET, such as the array, the list, and the stack, support these interfaces. A data contract can include a collection as a data member, and a service contract can define operations that interact with a collection directly. Because .NET collections are .NET-specific, WCF cannot expose them in the service metadata, yet because they are so useful, WCF offers dedicated marshaling rules for collections.

Whenever you define a service operation that uses the collection interfaces IEnumerable<T>, IList<T>, or ICollection<T>, the resulting metadata always uses an array. For example, this service contract definition and implementation:

[ServiceContract]
interface IContactManager
{
   [OperationContract]
   IEnumerable<Contact> GetContacts();
   ...
}
class ContactManager : IContactManager
{
   List<Contact> m_Contacts = new List<Contact>();

   public IEnumerable<Contact> GetContacts()
   {
      return m_Contacts;
   }
   ...
}

will still be exported as:

[ServiceContract]
interface IContactManager
{
   [OperationContract]
   Contact[] GetContacts();
}

Concrete Collections

If the collection in the contract is a concrete collection (not an interface) and is a serializable collection—that is, it is marked with the Serializable attribute but not with the DataContract attribute—WCF can normalize the collection automatically to an array of the collection’s type, provided the collection contains an ...

Get Programming WCF Services, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.