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

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.