Скачиваний:
64
Добавлен:
15.03.2015
Размер:
4.31 Mб
Скачать

316

Day 13

If the remote Web Service resides on another machine, the localhost string in Listing 13.5 changes to the name of that machine. After running the project, you should see a screen similar to Figure 13.5.

FIGURE 13.5

The ASP.NET client for the Web Service.

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 13.6 shows how SOAP is used with these other two protocols between a Web Service and its client.

FIGURE 13.6

 

 

 

 

 

+ - -

-

- - - - - -

+

 

 

 

 

 

 

 

 

 

WSDL

 

 

Internet protocols used

- -+

- - - - - - - +

 

- - - - - -+

 

- - -

- - - - -+

+

by a Web Service and

 

 

Client

 

 

 

Server

 

 

 

 

+

+

SOAP

+

 

 

+

 

 

its clients.

-

- -

 

 

 

 

 

- - - - - - - - -

HTTP - - - - - - - - -

-

 

 

 

 

 

 

 

 

 

 

 

- - - - - - - - -

TCP - - - - - - - - - - -

 

 

 

 

 

 

 

 

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 13.6 shows the SOAP message that a client sends to the TimeUtils Web Service.

LISTING 13.6 A 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>

ANALYSIS

Introducing Web Services

317

From Listing 13.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 1–5) in Listing 13.7 are placed before the SOAP message (Lines 2–8) in Listing 13.6. The HTTP request using SOAP for the TimeUtils looks like Listing 13.7.

LISTING 13.7 An 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”

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

13

and dispatch SOAP requests to your Web server.

Note

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.

318

Day 13

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 today, 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.

Summary

Today’s lesson explained how to create and use Web Services. Now that you know the basics of Web Services and how they work, you are well equipped to learn more advanced techniques to use with Web Services, such as sending and returning custom data structures and ADO.NET datasets.

Today you learned how to create Web Service proxies with the .NET framework tools and automatically with Visual Studio.NET. You learned how a client program can use proxy class to call remote Web Services.

You also learned how Web Services communicate with client programs using the SOAP protocol. You saw examples of SOAP requests and learned why Web Services are used together with Web servers such as IIS.

Introducing Web Services

319

Q&A

QIf I implement a Web Service using ASP.NET, will my service’s clients be tied to the Microsoft platform?

ANo. You can use any platform and language combination that supports TCP/IP. This makes your choice of client implementation almost unlimited. Client operating systems can include Windows 3.11 through Windows XP, all flavors of Unix and Linux, VAX, mobile device operating systems, and many others. Your choice of language to use for the client program could include Java, C, C++, Visual Basic, or any language that includes TCP/IP support.

QHow much about the SOAP protocol do I need to know to create Web Services?

AIf you use .NET to create Web Services and their clients, you don’t need to know much more than what you’ve seen in this chapter. ASP.NET and the .NET framework provide all the SOAP protocol plumbing for you. This situation is similar to the HTTP protocol and Web site programming, in that you can create most Web pages without detailed knowledge of HTTP requests and responses. To learn more about the SOAP protocol, visit C:\mcp\BOOK0501\REVIEW\235813c2.dochttp://

www.w3c.org.

Workshop

The workshop provides quiz questions to help you solidify your understanding of the material covered today as well as exercises to give you experience using what you have learned. Try to understand the quiz and exercise before continuing to tomorrow’s lesson. Answers are provided in Appendix A, “Answers to Quizzes and Exercises.”

Quiz

1. What file extension does ASP.NET use for Web Services?

13

 

2.Why do Web Services use WSDL files?

3.How do you retrieve the WSDL description for an ASP.NET Web Service?

4.Why should you use proxies for Web Service clients?

5.What files are created when you select Add Web Reference in Visual Studio?

6.Why do Web Services use ASP.NET?

7.Do all Web Services support HTTP Get and HTTP Post for calling functions?

320

Day 13

8.List each standard in today’s lesson that uses XML for its data format.

9.If a Web Service offers a method called HelloWorld, what two methods would a proxy program define for asynchronous calling?

10.What programming attribute do you use to make a method accessible to the public in a Web Service?

Exercises

1.Create a new method in the TimeUtils Web Service that returns the time of day in military and standard format. Have the method receive a bool parameter that specifies which format to use.

2.Create a Web Service using Visual Studio. What new files did Visual Studio create that we didn’t in the TimeUtils example?

3.Create a Web Service that calls another Web Service. How could a Web Service like this be useful?

WEEK 2

DAY 14

Publishing Web Services

After you start to create your own Web Services, you will need a way to let the rest of the world know about them. Today’s lesson will explain how clients discover Web Services and how to publish information about your Web Services.

The first section of today’s lesson will explain exactly how the Add Web Reference dialog works in Visual Studio and will examine Web Service discovery files. These files can be used so that when Web Service consumers know the domain name of a Web server, they can easily find the complete specifications for all the services that the server provides.

Today’s lesson will then introduce UDDI, an industry standard for listing Web Services with widespread support from many key leaders in the software industry. Even Microsoft and Sun, along with more than 80 major industry leaders, agree that UDDI should be the standard for Web Service discovery. You will learn about the structure and content of entries in the UDDI registry and see how to create entries for your own organization’s Web Services.

Today’s lesson covers the following topics:

Dynamic and static Web Service discovery files and tips for how to choose between them

UDDI, including an introduction and explanation of the UDDI registry