O'Reilly logo

Programming WCF Services, 2nd Edition by Juval Lowy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 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 Add( ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required