This tutorial shows how to create and use Web Services in C#.
We begin with an a general overview of .NET Web services concepts and
technologies used to implement Web services. Next, we present a step-by-step
walkthrough of building and deploying a HugeInteger
Web service that performs calculations
with integers up to 100 digits long. We then build a Windows application
that consumes the Web service. This
tutorial is intended for students who are already familiar with C# and for C# developers. Download the code exaples for this tutorial here.
[Note: This is an excerpt (Sections 22.1–22.4) of Chapter 22, Web Services, from our textbook Visual C# 2005 How to Program, 2/e. These articles may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., VISUAL C# 2005 HOW TO PROGRAM, 2/E, ©2005, pp.1164–1190. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]
This chapter introduces Web services, which promote software reusability in distributed systems where applications execute across multiple computers on a network. A Web service
is a class that allows its methods to be called by methods on other machines via common data formats and protocols, such as XML (see Chapter 19) and HTTP. In .NET, the over-the-network method calls are commonly implemented through the Simple Object Access Protocol (SOAP)
, an XML-based protocol describing how to mark up requests and responses so that they can be transferred via protocols such as HTTP. Using SOAP, applications represent and transmit data in a standardized XML-based format.
Microsoft is encouraging software vendors and e-businesses to deploy Web services. As increasing numbers of organizations worldwide have connected to the Internet, the concept of applications that call methods across a network has become more practical. Web services represent the next step in object-oriented programming-rather than developing software from a small number of class libraries provided at one location, programmers can access Web service class libraries distributed worldwide.
Web services are not the best solution for certain performance-intensive applications, because applications that invoke Web services experience network delays. Also, data transfers are typically larger because data is transmitted in text-based XML formats.
Web services facilitate collaboration and allow businesses to grow. By purchasing Web services and using extensive free Web services that are relevant to their businesses, companies can spend less time developing new applications. E-businesses can use Web services to provide their customers with enhanced shopping experiences. Consider an online music store. The store's Web site provides links to information about various CDs, enabling users to purchase the CDs or to learn about the artists. Another company that sells concert tickets provides a Web service that displays upcoming concert dates for various artists, then allows users to buy tickets. By consuming the concert-ticket Web service on its site, the online music store can provide an additional service to its customers and increase its site traffic. The company that sells concert tickets also benefits from the business relationship by selling more tickets and possibly by receiving revenue from the online music store for the use of its Web service. Many Web services are provided at no charge. For example, Amazon and Google offer free Web services that you can use in your own applications to access the information they provide.
Visual Web Developer and the .NET Framework provide a simple, user-friendly way to create Web services. In this chapter, we show how to use these tools to create, deploy and use Web services. For each example, we provide the code for the Web service, then present an application that uses the Web service. Our first examples analyze Web services and how they work in Visual Web Developer. Then we demonstrate Web services that use more sophisticated features, such as session tracking (discussed in Chapter 21) and manipulating objects of user-defined types.
As in Chapter 21, we distinguish between Visual C# 2005 Express and Visual Web Developer 2005 Express in this chapter. We create Web services in Visual Web Developer, and we create client applications that use these Web services using both Visual C# 2005 and Visual Web Developer 2005. The full version of Visual Studio 2005 includes the functionality of both Express editions.