Understanding How Web Services Work
Web Services communicate with clients using SOAP (Simple Object Access Protocol), which is built on top of the HTTP and TCP Internet protocols. Figure 6 shows how SOAP is used with these other two protocols between a Web Service and its client.
Figure 6 Internet protocols used by a Web Service and its clients.
SOAP uses XML and defines how a function should be called on a remote machine. It also specifies how parameters should be passed to and from a function on a remote machine. For example, Listing 6 shows the SOAP message that a client sends to the TimeUtils Web Service.
Listing 6A SOAP Request to the TimeUtils Web Service
1: <?xml version="1.0" encoding="utf-8"?> 2: <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3: xmlns:xsd="http://www.w3.org/2001/XMLSchema" 4: xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 5: <soap:Body> 6: <GetTime xmlns="http://tempuri.org/webservices" /> 7: </soap:Body> 8: </soap:Envelope>
From Listing 6, you can see that each SOAP request is an XML file that contains the name of a method to call and any parameters that the method might need. Because SOAP uses the HTTP protocol, the HTTP headers (Lines 15) in Listing 7 are placed before the SOAP message (Lines 28) in Listing 6. The HTTP request using SOAP for the TimeUtils looks like Listing 7.
Listing 7An HTTP Request Containing a SOAP Method Call
1: POST /timeservice/timeutils.asmx HTTP/1.1 2: Host: localhost 3: Content-Type: text/xml; charset=utf-8 4: Content-Length: 484 5: SOAPAction: "http://tempuri.org/webservices/GetTime" 6: 7: <?xml version="1.0" encoding="utf-8"?> 8: <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 9: xmlns:xsd="http://www.w3.org/2001/XMLSchema" 10: xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 11: <soap:Body> 12: <GetTime xmlns="http://tempuri.org/webservices" /> 13: </soap:Body> 14: </soap:Envelope>
Because SOAP uses HTTP, you can create a program that responds to SOAP requests using a Web server. This is why Web Services promise to become a popular way to create distributed programs. Currently, a huge number of Web servers are already in place, and given the right plumbing utilities, each one can be made to accept SOAP calls and return SOAP responses.
Because Web Services use SOAP and can run using Web servers, all .NET Web Services use ASP.NET as a foundation. ASP.NET is designed so that it can handle SOAP requests and dispatch them to your custom Web Services. If you create a Web Service using an ASP.NET page with an .asmx extension, ASP.NET will recognize it as a Web Service and dispatch SOAP requests to your Web server.
As you might suspect, you can use SOAP without a Web server. By using .NET, you can also create server programs that use SOAP and communicate with client programs without the use of a Web server. This process, called remoting in .NET, is beyond the scope of this book. However, the documentation that comes with Visual Studio.NET contains detailed information and examples on how to set up servers using SOAP. Look under the Remoting keyword.
Finding out how to communicate with a remote Web Service is difficult unless there's a standard way to find out what methods the Web Service offers. As you saw earlier in this chapter, WSDL files supply this information. Any developer who needs to use a Web Service can look at the WSDL file and then build programs that call the remote Web Service. Of course, .NET automates this feature with the WSDL.exe tool and Visual Studio.NET.
The Web Services Vision
If the programming community adopts Web Services, expect to see many companies supplementing their Web sites with Web Services. They could include airline reservation services, music and book pricing and shipping services, and a host of other services that Web sites provide now.
You may be in a position to supplement your own company's Web or intranet site with a Web Service. For instance, if your company's intranet provides phone book and contact directories, you might implement a Web Service that provides the same information. This would allow other developers in your company to create desktop applications or other Web pages that access this contact information.
If you are a more ambitious developer, you may be contemplating a Web Service that provides a service to the general public, such as music CDs. Whatever your goal, the .NET framework provides all the tools you will need to create a Web Service of any size.