304Microsoft Visual Studio 2010: A Beginner’s Guide

Public Interface ICustomerService

<OperationContract()>

Function GetData(ByVal value As Integer) As String

<OperationContract()>

Function GetDataUsingDataContract(

ByVal composite As CompositeType) As CompositeType

'TODO: Add your service operations here End Interface

'Use a data contract as illustrated in the sample below

'to add composite types to service operations

<DataContract()>

Public Class CompositeType

<DataMember()>

Public Property BoolValue() As Boolean

<DataMember()>

Public Property StringValue() As String

End Class

There are two types in Listing 11-1: ICustomerService and CompositeType. Both of these types were generated by VS to provide an example of how you can define a service contract. After explaining the default code, we’ll modify the code to make it usable for working with Customer objects.

Starting with the ICustomerService interface, the two most important parts of the code are the ServiceContract and OperationContract attributes. The ServiceContract attribute states that this interface defines a contract for a WCF Web service. Without the ServiceContract attribute, this interface won’t be recognized by WCF. The OperationContract attribute specifies methods that are exposed by the WCF service. Without the OperationContract attribute, a method will not be visible as part of the WCF service.

A WCF service method can work with any of the built-in types for parameters or return types, demonstrated by the GetData method that takes an int parameter and returns a string. When working with custom types, you need additional syntax to specify what parts of the type are part of the contract. The types are parameters and return types of the service methods, and are part of the contract in addition to the interface.

Page 327
Image 327
Microsoft 9GD00001 manual Microsoft Visual Studio 2010 a Beginner’s Guide