Dec. 21, 2007, 9:05 a.m.
posted by fractal
Understanding Web Services
Before I get into the nuts and bolts of actually working with Web services, I'll give you an overview of the way they work. The key to understanding Web services is to know something about the protocols that make them possible:
NOTE Naming Web Services As with many rapidly-developing technologies, different companies use different names for Web services. Some prefer the capitalized form "Web Services," and others use the explicit "XML Web Services." In this book, I've gone along with the MCAD exam objectives guide, which calls them "Web services." One important thing to realize is that, by default, all communication between Web services servers and their clients is through Extensible Markup Language (XML) messages transmitted over the Hypertext Transfer Protocol (HTTP) protocol. This has two benefits. First, because Web services messages are formatted as XML, they're reasonably easy for human beings to read and understand. Second, because those messages are transmitted over the pervasive HTTP, they can normally reach any machine on the Internet without being blocked by firewalls. SOAPFor Web services to manipulate objects through XML messages, there has to be a way to translate objects (as well as their methods and properties) into XML. SOAP is a way to encapsulate object calls as XML sent via HTTP. EXAM TIP SOAP over Other Protocols You often read that SOAP messages travel over HTTP. Although this is the default for SOAP as implemented by Visual Studio .NET, it's not a part of the SOAP specification. SOAP messages could be sent by email or File Transfer Protocol (FTP) without losing their content. As a practical matter, SOAP today uses HTTP in almost all cases. There are two major advantages to using SOAP to communicate with Web services. First, because HTTP is so pervasive, it can travel to any point on the Internet, regardless of intervening hardware or firewalls. Second, because SOAP is XML based, it can be interpreted by a wide variety of software on many operating systems. Although you'll only work with the Microsoft implementation of Web services in this chapter, numerous Web services tools from other vendors can interoperate with Microsoft-based Web services. Here's a typical SOAP message sent from a Web services client to a Web services server:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
xmlns:soap=
"http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc=
"http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns=
"http://www.capeclear.com/AirportWeather.wsdl"
xmlns:types="http://www.capeclear.com/
AirportWeather.wsdl/encodedTypes"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body soap:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/">
<q1:getLocation
xmlns:q1="capeconnect:AirportWeather:Station">
<arg0 xsi:type="xsd:string">KSEA</arg0>
</q1:getLocation>
</soap:Body>
</soap:Envelope>
Even without digging into this file in detail, you can see some obvious points:
Here's the SOAP message that comes back from the server:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV=
"http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:cc1=
"http://www.capeclear.com/AirportWeather.xsd"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC=
"http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body SOAP-ENV:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/">
<cc2:getLocationResponse
xmlns:cc2="capeconnect:AirportWeather:Station"
SOAP-ENC:root="1">
<return xsi:type="xsd:string">
Seattle, Seattle-Tacoma International Airport,
WA, United States</return>
</cc2:getLocationResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In the response message, the getLocationResponse element is the result of the call to the object on the server. It includes a string wrapped up as an XML element. Disco and UDDIBefore you can use a Web service, you need to know where to find the service. Handling such requests is the job of several protocols, including Disco and UDDI. These protocols enable you to communicate with a Web server to discover the details of the Web services that are available at that server. You'll learn more about these protocols later in the chapter. WSDLAnother prerequisite for using a Web service is knowledge of the SOAP message types that it can receive and send. You can obtain this knowledge by parsing WSDL files. WSDL is a standard by which a Web service can tell clients what messages it accepts and which results it will return. Here's a portion of a WSDL file:
<?xml version="1.0" encoding="utf-16"?>
<definitions
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:s0=
"http://www.capeclear.com/AirportWeather.xsd"
xmlns:soapenc=
"http://schemas.xmlsoap.org/soap/encoding/"
xmlns:tns=
"http://www.capeclear.com/AirportWeather.wsdl"
xmlns:tm=
"http://microsoft.com/wsdl/mime/textMatching/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
targetNamespace=
"http://www.capeclear.com/AirportWeather.wsdl"
name="AirportWeather"
xmlns="http://schemas.xmlsoap.org/wsdl/">
<types>
<s:schema targetNamespace=
"http://www.capeclear.com/AirportWeather.xsd">
<s:complexType name="WeatherSummary">
<s:sequence>
<s:element minOccurs="1" maxOccurs="1"
name="location" nillable="true"
type="s:string" />
<s:element minOccurs="1" maxOccurs="1"
name="wind" nillable="true"
type="s:string" />
<s:element minOccurs="1" maxOccurs="1"
name="sky" nillable="true"
type="s:string" />
<s:element minOccurs="1" maxOccurs="1"
name="temp" nillable="true"
type="s:string" />
<s:element minOccurs="1" maxOccurs="1"
name="humidity" nillable="true"
type="s:string" />
<s:element minOccurs="1" maxOccurs="1"
name="pressure" nillable="true"
type="s:string" />
<s:element minOccurs="1" maxOccurs="1"
name="visibility" nillable="true"
type="s:string" />
</s:sequence>
</s:complexType>
</s:schema>
</types>
<message name="getHumidity">
<part name="arg0" type="s:string" />
</message>
<message name="getHumidityResponse">
<part name="return" type="s:string" />
</message>
<message name="getLocation">
<part name="arg0" type="s:string" />
</message>
<message name="getLocationResponse">
<part name="return" type="s:string" />
</message>
<message name="getOb">
<part name="arg0" type="s:string" />
</message>
...
</definitions>
EXAM TIP Exposure Is Optional Although UDDI and WSDL files make it possible to interact with Web services without any prior knowledge, these files are not required for a Web service to function. You can make a Web service available on the Internet without any UDDI or WSDL file. In that case, only clients who know the expected message formats and the Web service's location are able to use it. WSDL files define everything about the public interface of a Web service, including the following:
Invoking Your First Web ServiceAt this point, I'd like to show you a Web service in action. Step-by-Step 4.1 shows how you can use a Web service, in this case one that supplies the weather at any airport worldwide. WARNING Working with the Internet Most of the examples in this chapter assume that you're working on a computer that is connected to the Internet. It's okay if there's a proxy server between you and the Internet, as long as you can connect to Web sites.
You'll learn more about the techniques in Step by Step 4.1 in the rest of the chapter, but you should be able to see the broad outlines of Web services already. In one sense, there's not much new here, compared to invoking any other object. After you've set a reference to the server, you can create objects from that server, invoke their methods, and examine the results. You could do the same with objects from a .NET library on your own computer. But in another sense, there's a lot of revolutionary work going on here, even though you don't see most of it happening. When you create the Web reference, for example, Visual Studio .NET reads the appropriate WSDL file to determine which classes and methods are available from the remote server. When you call a method on an object from that server, the .NET infrastructure translates your call and the results into SOAP messages and transmits them without any intervention on your part. Depending on the speed of your Internet connection, you may notice that the Web service client that you created in Step-by-Step 4.1 "freezes" when you invoke the Web service. This program uses a synchronous method to communicate with the Web service, waiting for the SOAP response before allowing any other code to execute. You will learn later in this chapter, in Exercise 4.1 and in Chapter 5, "Advanced Web Services," how to call a Web service asynchronously to increase the responsiveness of the client program.
|
- Comment


