Collections
In .NET, a collection is any type that supports the IEnumerable
or IEnumerable<T>
interfaces. 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, or 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’re defining a service operation that uses any of the following collection interfaces: IEnumerable<T>
, IList<T>
, and ICollection<T>
, the wire representation 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 ...
Get Programming WCF Services 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.