08.01.2015 Views

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

168<br />

CHAPTER 7 ■ .<strong>NET</strong> 3.0: WINDOWS COMMUNICATION FOUNDATION<br />

Creating an Address Service in WCF<br />

In this section, you’ll look at what it takes to create an address service similar to that in<br />

earlier chapters. However, as this is a WCF service, you’ll be able to take it to the next level<br />

with some simple configuration changes—for example, adding reliability, security, <strong>and</strong><br />

transactable characteristics to it.<br />

First, create the service using the WCF Service template in the File – New dialog.<br />

Next, add a new DataSet to it. The DataSet should be based on the following SQL, which<br />

makes a parameterized query against the database, filtering the addresses by the ZIP<br />

code associated with it. Here’s the SQL:<br />

SELECT<br />

FROM<br />

WHERE<br />

AddressLine1, City, PostalCode<br />

Person.Address<br />

(PostalCode = @ZIP)<br />

Chapter 5 shows the steps required to add this DataSet, if you aren’t already familiar<br />

with it. You’ll be using the same DataSet here as you used in that chapter when you created<br />

a web service.<br />

The first thing you’ll need to do is create the interface that defines your service contract.<br />

In the Chapter 5 web service, you passed an XmlDocument type, which is a complex<br />

object requiring some pretty sophisticated data contracts to be set up in order to use it.<br />

For the sake of simplicity in this example, you’ll instead pass a string. As a string is a<br />

simple data type, you don’t need to define a data contract for it.<br />

However, you’ll still need the ServiceContract to define the interface, <strong>and</strong> an<br />

OperationContract to define the methods that are consumable by a client. Here’s<br />

the code:<br />

[ServiceContract()]<br />

public interface IAddressService<br />

{<br />

[OperationContract]<br />

string GetAddresses(string strZIP);<br />

}<br />

Next, you build your class that implements this interface. This class will also implement<br />

the specified operation (GetAddresses).<br />

First, here’s the class description:<br />

public class AddressService : IAddressService<br />

{<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!