03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

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

Us<strong>in</strong>g Web Services <strong>and</strong> Page Methods <strong>in</strong> Ajax Websites x 369<br />

The web services <strong>in</strong> the Planet Wrox project will only be used to have a client page <strong>in</strong> the browser<br />

talk to the server <strong>and</strong> exchange data. So, <strong>in</strong> this site, both the server <strong>and</strong> the client are <strong>in</strong> the same<br />

web project—one executes at the client (the JavaScript that calls the web server), <strong>and</strong> the other lives<br />

at the server (the web service itself). From a security po<strong>in</strong>t of view, this is the easiest solution because<br />

both parts trust each other.<br />

If you want your client-side pages to talk to a web service on a different doma<strong>in</strong>, you could host<br />

a service on your own site that calls the remote web service. The client browser then calls your<br />

service, which <strong>in</strong> turns calls the remote service. This scenario falls outside the scope of this chapter,<br />

though.<br />

You see this WCF theory <strong>in</strong> practice <strong>in</strong> the follow<strong>in</strong>g exercise.<br />

Creat<strong>in</strong>g Web Services<br />

Creat<strong>in</strong>g WCF services with VS is pretty easy. Just as with all the other document types, VS comes<br />

with a template for a WCF service. You add the service to the site us<strong>in</strong>g the Add New Item dialog<br />

box. You then modify the service code to suit your requirements. Next, you register the service <strong>in</strong> a<br />

ScriptManager or ScriptManagerProxy <strong>and</strong> then you’re ready to call it from a client web page.<br />

VS comes with a few different templates to create a WCF service. To create one that’s callable<br />

from a website, you use the AJAX-enabled WCF Service template. When you add a service based<br />

on this template, VS adds the necessary configuration code to allow call<strong>in</strong>g this service from a<br />

web page to the Web.config file for you. In addition, the cod<strong>in</strong>g model of this service is a bit<br />

easier than the st<strong>and</strong>ard WCF Service template because it stores all the code <strong>in</strong> a s<strong>in</strong>gle class<br />

<strong>and</strong> file.<br />

TRY IT OUT<br />

Creat<strong>in</strong>g a Web Service<br />

In this exercise you create a simple “Hello World” web service. This service accepts your name as an<br />

<strong>in</strong>put parameter <strong>and</strong> returns a friendly, personalized greet<strong>in</strong>g. There’s not much real-world usage for<br />

this exact web service, but because of the simplicity <strong>in</strong> the service itself, it’s easy for you to focus on the<br />

underly<strong>in</strong>g concepts.<br />

1. Create a new folder called WebServices <strong>in</strong> the root of your site to group all web services <strong>in</strong> a<br />

s<strong>in</strong>gle folder. This is not required, but helps <strong>in</strong> organiz<strong>in</strong>g your site.<br />

2. Next, right-click this new folder <strong>and</strong> choose Add Í Add New Item. Click the AJAX-enabled<br />

WCF Service item. Because the list of templates can be quite long, you can quickly f<strong>in</strong>d the<br />

right item by search<strong>in</strong>g for WCF <strong>in</strong> the search box <strong>in</strong> the top-right corner of the Add New Item<br />

dialog box.<br />

Make sure that you click the item for your programm<strong>in</strong>g language, <strong>and</strong> call the service<br />

NameService, as shown <strong>in</strong> Figure 10-6.

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

Saved successfully!

Ooh no, something went wrong!