03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

364 x CHAPTER 10 <strong>ASP</strong>.<strong>NET</strong> AJAX<br />

What Are Web Services?<br />

Web services are essentially methods that you can call over the Internet <strong>and</strong> that can optionally<br />

return data to the call<strong>in</strong>g code. This makes them ideal for exchang<strong>in</strong>g data between different systems.<br />

Because web services are based on solid <strong>and</strong> well-understood st<strong>and</strong>ards, they make it easy to<br />

exchange data between different types of platforms <strong>and</strong> systems. For example, with a web service<br />

it’s possible to exchange data between an <strong>ASP</strong>.<strong>NET</strong> website runn<strong>in</strong>g on Microsoft W<strong>in</strong>dows <strong>and</strong> a<br />

PHP-based site runn<strong>in</strong>g on L<strong>in</strong>ux. But at the same time, it’s also possible to exchange data between<br />

an <strong>ASP</strong>.<strong>NET</strong> or PHP website <strong>and</strong> a client browser us<strong>in</strong>g JavaScript.<br />

Introduc<strong>in</strong>g WCF<br />

To build web services <strong>in</strong> an <strong>ASP</strong>.<strong>NET</strong> website, you use W<strong>in</strong>dows Communication Foundation<br />

(WCF), Microsoft’s platform for service-oriented applications us<strong>in</strong>g the .<strong>NET</strong> Framework. In previous<br />

versions of <strong>ASP</strong>.<strong>NET</strong> you could also make use of so-called ASMX web services, but these have<br />

now been deprecated <strong>in</strong> favor of WCF. However, this isn’t really a problem because WCF can do<br />

anyth<strong>in</strong>g that ASMX web services could do <strong>and</strong> much more.<br />

WCF supports a number of different underly<strong>in</strong>g network communication technologies such as<br />

HTTP, .<strong>NET</strong> Remot<strong>in</strong>g, Microsoft Message Queu<strong>in</strong>g, <strong>and</strong> Enterprise Services. This makes it an<br />

ideal platform for the exchange of data <strong>in</strong> a variety of scenarios such as locally on a s<strong>in</strong>gle mach<strong>in</strong>e,<br />

on a corporate network, or over the Internet. For public-fac<strong>in</strong>g websites such as the Planet Wrox<br />

site, HTTP or HTTPS (the secured version of HTTP) is the natural choice because it will work<br />

cross-browser <strong>and</strong> across firewalls.<br />

For <strong>in</strong>formation about the other supported technologies, check out this article on the MSDN<br />

website at http://msdn.microsoft.com/library/dd943056.aspx or get a copy of Professional<br />

WCF 4: W<strong>in</strong>dows Communication Foundation with .<strong>NET</strong> 4 (Wrox, ISBN: 978-0-470-56314-4).<br />

To build a WCF web service, you add a WCF service (with an .svc extension) to your project. As<br />

you see later, you have a few different templates available, each serv<strong>in</strong>g a different purpose. Inside<br />

this service file you def<strong>in</strong>e a Service Contract <strong>and</strong> an Operation Contract. The Service Contract<br />

def<strong>in</strong>es the overall service <strong>and</strong> the Operation Contract def<strong>in</strong>es the various methods that are available<br />

on the service. The follow<strong>in</strong>g snippet shows a simple WCF service with a s<strong>in</strong>gle method:<br />

<strong>VB</strong>.<strong>NET</strong><br />

<br />

<br />

Public Class NameService<br />

<br />

Public Function HelloWorld(name As Str<strong>in</strong>g) As Str<strong>in</strong>g<br />

Return Str<strong>in</strong>g.Format("Hello {0}", name)<br />

End Function<br />

End Class<br />

C#<br />

[ServiceContract(Namespace = "")]<br />

[AspNetCompatibilityRequirements(

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

Saved successfully!

Ooh no, something went wrong!