310Microsoft Visual Studio 2010: A Beginner’s Guide

The methods of the CustomerService class in Listing 11-4 show skeleton implementations of the ICustomerService interface. As you know, Listing 11-2 provided new methods to

the ICustomerService interface, so the code in Listing 11-4 will not compile because it doesn’t implement the ICustomerService methods. To fix this problem, delete the GetData and GetDataUsingDataContract methods from the CustomerService class. Then select the ICustomerService identifier in the CustomerService.cs file, which will display an underline on the left of the ICustomerService identifier. Hover over that underline to open a menu with an option to implement the ICustomerService interface, which will generate skeleton code for each member of the ICustomerService interface inside of the CustomerService class. The default method implementations throw a NotImplementedException exception, meaning that you need to write the code to implement those methods based on the ICustomerService interface. Listing 11-5 shows the implementation of the ICustomerService interface in

the CustomerService class. If using C#, add the code to each method. If using VB, which doesn’t have the same interface refactoring support as C#, add all methods and code to the CustomerService class as specified in Listing 11-5.

Listing 11-5 A WCF service implementation

C#:

using System;

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

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

using System.Text;

namespace WcfDemoCS

{

public class CustomerService : ICustomerService

{

public Customer GetCustomer(int custID)

{

var ctx = new MyShopDataContext();

var customer =

(from cust in ctx.Customers where cust.CustomerID == custID select cust)

.SingleOrDefault();

Page 333
Image 333
Microsoft 9GD00001 manual Listing 11-5 a WCF service implementation