Chapter 11: Deploying Web Services with WCF

305

The GetDataUsingDataContract method illustrates a method that uses a custom type, CompositeType, as a parameter and return type. Being a custom type, CompositeType has attributes that help define its contract: DataContract and DataMember. The DataContract attribute identifies CompositeType as a type that can be included in a WCF service contact. Without the DataContract attribute, a type can’t be included as part of the service contract. The DataMember attribute decorates type members that are part of the contract for this service. Without the DataMember attribute, a type member will not be visible as part of the contract.

Creating Your Own Contract

We won’t explicitly construct our data types for DataContracts, as shown in the CompositeType in Listing 11-1. Instead, we’ll use a built-in capability of LINQ to SQL that gives LINQ to SQL entities a DataContract. To use LINQ to SQL entities, create a new LINQ to SQL item in the same project the WCF service resides in and add the Customer table to the designer. Then click the design surface, not the Customer entity, and view properties. Set Serialization Mode to Unidirectional, as shown in Figure 11-2.

Now, instead of creating a custom type and copying LINQ to SQL entity data into the custom type and returning the custom type, it will be possible to perform a LINQ to SQL query and return the LINQ to SQL entity.

Figure 11-2 Setting the LINQ to SQL Serialization Mode property

Page 328
Image 328
Microsoft 9GD00001 manual 305, Creating Your Own Contract