Returning ReadOnlyCollection data

It is quite common to return a collection of data objects in a WCF service operation. However, sometimes we need to return a list of objects that are stored in the ReadOnlyCollection<T> type instance. This cannot be done directly, since ReadOnlyCollection<T> is not supported by the WCF DataContractSerializer. To make a WCF service operation return an ReadOnlyCollection<T> collection, we need to perform some customization on the OperationContract.

How to do it...

To return ReadOnlyCollection<T> collection instances, we need to modify our WCF service operation's OperationContract—the return type should be changed to use a base/generic Collection type other than ReadOnlyCollection<T>. For example, in our sample service, ...

Get Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications 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.