302Microsoft Visual Studio 2010: A Beginner’s Guide

The WCF Service Library starts with two files with default names of IService1.cs (IService1.vb for VB) and Service1.cs (Service1.vb for VB), which contain an interface and a class that implements that interface. If you need to brush up on interfaces, review Chapter 4 because an interface is an integral part of WCF development.

Specifying a Contract with WCF Interfaces

The IService1.cs (IService1.vb in VB) class in Figure 11-1 contains an interface. As you learned in Chapter 4, interfaces define a set of members that do not have implementations. The actual implementation will be provided by classes that implement the interface. You can consider the interface to be a contract that guarantees a set of operations for a service. In addition to the interface, the types associated with the service are part of the service contract. The contract is important because when you write code that uses the Web service, it is the contract that your code will see and everything that is not in the contract will not be visible. Any application wishing to use a Web service will make calls to the Web service based on what is specified in the contract. In this section, you’ll see how to define a WCF contract with an interface, built-in types, and custom types. Later sections will show how to implement and consume the contract, bringing the importance of the contract full circle so that you can see how the contract is defined, implemented, and consumed.

Examining the VS-Generated Contract

You really don’t want to work with an interface named IService1; it doesn’t mean anything. So, rename IService1.cs to ICustomerService.cs (IService1.vb to ICustomerService.vb for VB), because it will be configured to manage customer records. You’ll receive a message box for renaming the code, and you should respond affirmatively. When you open the ICustomerService.cs file, you’ll see the same code as Listing 11-1, containing an interface and attributes for defining the ICustomerService contract.

Listing 11-1 A WCF service interface

using System;

using System.Collections.Generic; using System.Linq;

using System.Runtime.Serialization; using System.ServiceModel;

using System.Text; namespace WcfDemoCS

Page 325
Image 325
Microsoft 9GD00001 manual Specifying a Contract with WCF Interfaces, Examining the VS-Generated Contract