blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

John Rayner's Blog

Brain.Extract<IBloggable>( Where.Author.Is( "John.Rayner" ) );

WCF: Sending Collections Over the Wire

Recently I needed to send a custom collection class as a response to a WCF call.  I duly added the normal [DataContract] attribute and was rewarded with the following WCF error:

Type 'MyCollection' is an invalid collection type since it has DataContractAttribute attribute.

Hang on, you may think, why do we needed custom collection classes given all the great generic collections in .Net 2.0, e.g. List<T> and so on?  You have a point, but for reasons of their own some of my team members decided to create a class such as the following:

public class MyCollection : List<MyObject>

It turns out that [DataContract] is not the correct attribute to be using in this case.  WCF also provides a [CollectionDataContract] attribute.  So the following code works just fine:

[CollectionDataContract]
public class MyCollection : List<MyObject>

Published 24 October 2006 10:00 by john.rayner
Filed under: , ,

Comments

 

Anthony Steele's Blog said:

I spent a lot of the 1990s writing business objects, and as soon as we&#39;d made a &quot;Widget&quot;

November 15, 2006 12:54
 

LukeSkywalker said:

I strongly type my collections when they are part of APIs. If a collection is in a reusable type library or a framework, you should derive from the ObjectModel.Collection<RocketShip> (or whichever suits) to make a RocketShipCollection. It is more intuitive and you can add methods like LaunchAll() to it.

June 17, 2008 15:51
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems