O'Reilly logo

Programming WCF Services 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

Data Sets and Tables

One of the most common types of data contracts exchanged between clients and services is data that originates in or is destined to a database. In .NET, the native way of interacting with databases is via ADO.NET's data set and data table types. Applications can use the raw DataSet and DataTable types, or use the data access management tools in Visual Studio to generate type-safe derivatives.

The raw DataSet and DataTable types are serializable, marked with the Serializable attribute:

[Serializable]
public class DataSet : ...
{...}

[Serializable]
public class DataTable : ...
{...}

This means that you can define valid service contracts that accept or return data tables or data sets:

[DataContract]
struct Contact
{...}

[ServiceContract]
interface IContactManager
{
   [OperationContract]
   void AddContact(Contact contact);

   [OperationContract]
   void AddContacts(DataTable contacts);

   [OperationContract]
   DataTable GetContacts( );
}

When importing the definition of this service contract above, the generated proxy file will contain the definition of the DataTable data contract—only the schema of DataTable, without any of the code. You can freely remove this definition from the file and reference ADO.NET instead.

You can also use the type-safe subclasses of DataSet and DataTable in your contract. For example, suppose you have a table in a database called ContactsDataTable, containing your contacts, with columns such as FirstName and LastName. You can use Visual Studio to generate ...

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