51
Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

  • Upload
    fai

  • View
    34

  • Download
    3

Embed Size (px)

DESCRIPTION

Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC. Introduction. - PowerPoint PPT Presentation

Citation preview

Page 1: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Web Services

Dr. Awad Khalil

Computer Science & Engineering Department

AUC

Page 2: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

2

Introduction

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

Today, software vendors and e-businesses are encouraged to deploy Web services. An 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.

Page 3: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

3

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 consumers and increase its site traffic. The company that sells concert tickets also benefits from the online music store for the use of its Web service.

Many services are provided at no charge. For example, Amazon.com and Google offer free Web services that you can use in your own applications to access the information they provide.

Page 4: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

4

Web services have important implications for business-to-business (B2B) transactions. They enable businesses to conduct transactions via standardized, widely available Web services rather than relying on proprietary applications.

Web services and SOAP are platform and language independent, so companies can collaborate via Web services without worrying about the compatibility of their hardware, software and communications technologies.

Companies such as Amazon.com, Google, eBay and many others are using Web services to their advantages.

Page 5: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

5

Limitations

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.

Visual Studio IDE and .NET Framework provide a simple, user-friendly way to create Web services. We shall see how to use these tools to create, deploy and use Web services.

Page 6: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

6

.NET Web Services Basics

A Web service is a software component stored on one machine that can be accessed by an application (or other software component) on another machine over a network.

The machine on which the Web service resides is referred to as a remote machine. The application (i.e., the client) that accesses the Web service sends a method call over the network to the remote machine, which processes the call and returns a response over the network to the application.

This kind of distributed computing benefits various systems. For example, an application without direct access to certain data on another system might be able to retrieve this data via a Web service. Similarly, an application lacking the processing power necessary to perform specific computations could use a Web service to take advantage of another system’s superior resources.

Page 7: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

7

A Web service is typically implemented as a class. When a client uses a Web service, the class (and its compiled DLL) is stored on a remote machine – a compiled version of the Web service is not placed in the current application’s directory.

Requests to and responses from Web services created with Visual Studio IDE are typically transmitted via SOAP. So any client capable of generating and processing SOAP messages can interact with a Web service, regardless of the language in which the Web service is written.

It is possible for Web services to limit access to authorized clients. There are standard mechanisms and protocols addressing Web service security issues.

Page 8: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

8

Creating a Web Service To create a Web service in Visual Studio IDE, you first create a project of type

ASP.NET Web Service. Visual Studio IDE then generates the following: files to contain the Web service code (which implements the Web service) An ASMX file (which provides access to the Web service) DISCO file (which potential clients use to discover the Web service) Visual Studio IDE generates code files for the Web service class and any other code

that is part of the Web service implementation. In the Web service class, you define the methods that your Web service makes available to client applications. Like ASP.NET Web applications, ASP.NET Web services can be tested using Visual Studio IDE’s built-in test server. However, to make an ASP.NET Web service publicly accessible to clients outside Visual Studio IDE, you must deploy the Web service to a Web server such as an Internet Information Services (IIS) Web server.

Methods in a Web service are invoked through a remote Procedure Call (RPC). These methods, which are marked with the WebMethod attribute, are often referred to as Web service methods or simply Web methods. Declaring a method with attribute WebMethod makes the method accessible to other classes through RPCs and is known as exposing a Web method.

Page 9: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

9

Discovering Web Services

Once you implement a Web service, compile it and deploy it on a Web server (explained later), a client application can consume (i.e. use) the Web service. However, clients be able to find the Web service and learn about its capabilities. Discovery of web services (DISCO) is a Microsoft-specific technology used to locate Web services on a server. Four types of DISCO files facilitate the discovery process: .disco files, .vsdisco files, .discomap files and .map files.

DISCO files consist of XML markup that describes for clients the location of Web services. A .disco file is accessed via a Web service’s ASMX page and contains markup specifying references to the documents that define various Web services. The resulting data that is returned from accessing a .disco file is placed in the .discomap file.

Page 10: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

10

A .vsdisco file is placed in Web a Web service’s application directory and behaves in a slightly different manner. When a potential client requests a .vsdisco file, XML markup describing the locations of Web services is generated dynamically, then returned to the client. First, the .NET Framework searches for Web services in the directory in which the .vsdisco file is located, as well as that directory’s subdirectories. The .NET Framework then generates XML (using the same syntax as that of a .disco file) that contains references to all the Web services found in this search.

Note that a .vsdisco file does not store the markup generated in response to a request. Instead, the .vsdisco file on disk contains configuration settings that specify the .vsdisco file’s behavior. For example, developers can specify in the .vsdisco file certain directories that should not be searched when a client requests a .vsdisco file. Although a developer can open a .vsdisco file in a text editor and examine its contents, this is rarely necessary – a .vsdisco file is intended to be requested (i.e., viewed in a browser) by clients over the Web. Every time this occurs, new markup is generated and displayed.

Page 11: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

11

Using .vsdico files benefits developers in several ways. These files contain only a small amount of data and provide up-to-date information about a server’s available Web services. However, .vsdisco files generate more overhead (i.e., require more processing) than .disco files do, because a search must be performed every time a .vsdisco file is accessed. Thus, some developers find it more beneficial to update .disco files manually. Many systems use both types of files. As we discuss shortly, Web services created using ASP.NET contain the functionality to generate a .disco file when it is requested. This .disco file contains references only to files in the current Web service. Thus, a developer typically places a .vsdisco file at the root of a server, when accessed, this file locates the .disco files for Web services anywhere on the system and uses the markup found in these .disco files to return information about the entire system.

Page 12: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

12

Determining a Web Service’s Functionality

After locating a Web service, the client must determine the Web service’s functionality and how to use it. For this purpose, Web services normally contain a service description. This is an XML document that conforms to the Web Service Description Language (WSDL) – an XML vocabulary that defines the methods a Web service makes available and how clients interact with them. The WSDL document also specifies lower-level information that clients might need, such as the required formats for requests and responses.

WSDL documents are not meant to be read by developers; rather, WSDL documents are meant to be read by applications, so they know how to interact with the Web services described in the documents. IDE generates an ASMX file when a Web service is constructed . Files with the .asmx filename extension are ASP.NET Web service files and are executed by ASP.NET on a Web server (e.g., IIS).

Page 13: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

13

When viewed in a Web browser, an ASMX file presents Web method descriptions and links to test pages that allow users to execute sample calls to these methods. The ASMX file also specifies the Web service’s implementation class, and optionally the code-behind file in which the Web service is defined and the assemlies referenced by the Web service.

When the Web server receives a request for the Web service, it accesses the ASMX file, which in turn, invokes the Web service implementation. To view more technical information about the Web service, developers can access the WSDL file (which is generated by ASP.NET).

The following ASMX page displays information about the HugeInteger Web service that we will create later. This Web service is designed to perform calculations with integers that contain a maximum of 100 digits. Most programming languages cannot easily perform calculations using integers this large.

Page 14: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

14

Page 15: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

15

The Web service HugeInteger provides client applications with methods that take two “huge integers” and determine their sum, their difference, which one is larger or smaller and whether the two numbers are equal.

Note that the top of the page provides a link to the Web service’s Service Description. ASP.NET generates the WSDL service description from the code you write to define the Web service. Client programs use a Web service’s service description to validate Web method calls when the client programs are compiled

ASP.NET generates WSDL information dynamically rather than creating an actual WSDL file. If a client requests the Web service’s WSDL description (either by appending ?WSDL to the ASMX file’s URL or by clicking the Service Description link), ASP.NET generates the WSDL description, then returns it to the client for display in the Web browser.

Generating the WSDL description dynamically ensures that clients receive the most current information about the Web service. It is common for an XML document (such as a WSDL description) to be created dynamically and not saved to disk. When a user clicks the Service Description link at the top of the ASMX page, the browser displays the generated WSDL document containing the service description.

Page 16: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

16

Page 17: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

17

Testing a Web Service’s Methods

Below the Service Description link, the ASMX page lists the methods that the Web service offers. Clicking any method name requests a test page that describes the method. The test page allows users to test the method by entering parameter values and clicking the Invoke button.

Below the Invoke button, the page displays sample request-and-response messages using SOAP and HTTP POST. These protocols are two options for sending and receiving messages in Web services.

The protocol that transmits request-and-response messages is also known as the Web service’s wire format or wire protocol, because it defines how information is sent “along the wire.”

SOAP is the more commonly used wire format, because SOAP messages can be sent using several transport protocols, whereas HTTP POST must use HTTP. When you test a Web service via an ASMX page, the ASMX page uses HTTP POST to test the Web service methods. Later, when we use Web services in our C# programs, we employ SOAP – the default protocol for .NET Web services.

Page 18: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

18

The following figure depicts the test page for the HugeInteger Web method Bigger. From this page, users can test the method by entering values in the first: and second: fields, then clicking Invoke. The method executes, and a new Web browser window opens, displaying an XML document that contains the result.

Page 19: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

19

Page 20: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

20

Building a Client to Use a Web Service A .NET client can be any type of .NET application, such as Windows

application, a console application or a Web application. You can enable a client application to consume a Web service by adding a Web reference to the client. This process adds files to the client application that allow the client to access the Web service.

To add a Web reference in Visual C#, right click the project name in the Solution Explorer and select Add Web Reference…. In the resulting dialog, specify the Web service to consume. Visual C# then adds an appropriate Web reference to the client application.

When you specify the Web service you want to consume, Visual C# accesses the Web service’s WSDL information and copies it into a WSDL file that is stored in the client project’s Web References folder. This file is visible when you instruct Visual C# to Show All Files.

Note that a copy of the WSDL file provides the client application with local access to the Web service’s description. To ensure that the WSDL file is up-to-date, Visual C# provides an Update Web Reference option which updates the files in the Web References folder.

Page 21: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

21

The WSDL information is used to create a proxy class, which handles all the “plumbing” required for Web method calls (i.e., networking details and the formation of SOAP messages).

Whenever the client application calls a Web method, the application actually calls a corresponding method in the proxy class. This method has the same name and parameters as the Web method that is being called, but formats the call to be sent as a request in a SOAP message.

The Web service receives this request as a SOAP message, executes the method call and sends back the result as another SOAP message.

When the client application receives the SOAP message containing the response, the proxy class deserializes it and returns the results as the return value of the Web method that was called.

The following figure depicts the interactions among the client node, proxy class and Web service.

The .NET environment hides most of these details from you. Many aspects of Web service creation and consumption – such as generating WSDL files, proxy classes and DISCO files – are handled by Visual Studio IDE.

Page 22: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

22

Simple Object Access Protocol (SOAP) The Simple Object Access Protocol (SOAP) is a platform-independent

protocol that uses XML to make remote procedure calls, typically over HTTP. Each request and response is packaged in a SOAP message – an XML message containing the information that a Web service requires to process the message.

SOAP messages are written in XML so that they are human readable and platform independent.

Most firewalls – security barriers that restrict communication among networks – do not restrict HTTP traffic. Thus, XML and HTTP enable computers on different platforms to send and receive SOAP messages with few limitations.

Web services also use SOAP for the extensive set of types it supports. The wire format used to transmit requests and responses must support all types passed between the applications. SOAP types include the primitive types (e.g., Integer), as well DateTime, XmlNode and others. SOAP can also transmit arrays of all these types. In addition, DataSets can be serialized into SOAP. You can also transmit user-defined types in SOAP messages.

Page 23: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

23

When a program invokes a Web method, the request and all relevant information are packaged in a SOAP message and sent to the server on which the Web service resides.

When the Web service receives this SOAP message, it begins to process the contents (contained in a SOAP envelope), which specify the method that the client wishes to execute and any arguments the client is passing to that method. This process of interpreting a SOAP message’s contents is known as parsing a SOAP message. After the Web service receives and parses a request, the proper method is called with the specified arguments (if there are any), and the response is sent back to the client in another SOAP message. The client parses the response to retrieve the result of the method call.

The following SOAP request was taken from the test page for the HugeInteger Web service’s Bigger method. Visual C# creates such a message when a client wishes to execute the HugeInteger Web service’s Bigger method. If the client is a Web application, Visual IDE creates the SOAP message. The message contains placeholders (length in line 4 and string in lines 16-17) representing values specific to a particular call to Bigger

Page 24: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

24

If this were a real SOAP request, elements first and second (lines 16-17) would each contain an actual value passed from the client to the Web service, rather than the placeholder string. For example, if this envelope were transmitting the request, element first and element second would contain the numbers and placeholder length (line 4) would contain the length of the SOAP message.

Most programmers do not manipulate SOAP messages directly, but instead allow the .NET framework to handle the transmission details.

Page 25: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

25

1. POST /HugeInteger/HugeInteger.asmx HTTP/1.1

2. Host: localhost

3. Content-Type: text/xml; charset=utf-8

4. Content-Length: length

5. SOAPAction: http://deitel.com/Bigger

6.

7. <?xml version=“1.0” encoding=“utf-8” ?>

8.

9. <soap:Envelope

10. xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

11. xmlns:xsd=http://www.w3.org/2001/XMLSchema

12. xmlns:soap=http://schemas.xmlsoap.org/soap/envelope/>

13.

14. <soap:body>

15. <Bigger xmlns=“http://www.deitel.com”>

16. <first>string</first>

17. <second>string</second>

18. </Bigger>

19. <soap:body>

20. </soap:Envelope>

Page 26: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

26

Publishing and Consuming Web Services

This section presents several examples of creating (also known as publishing) and using (also known as consuming) Web services.

Recall that an application that consumes a Web service actually consists of two parts – a proxy class representing the Web service and a client application that accesses the Web service via an instance of the proxy class.

The instance of the proxy class passes a Web method’s arguments from the client application to the Web service. When the Web service completes its task, the instance of the proxy class receives the result and parses it for the client application.

Page 27: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

27

Defining the HugeInteger Web Service

The following presents the code-behind for the HugeInteger Web service that we will create later.

When creating Web services in Visual Studio IDE, you work almost exclusively in the code-behind file.

The HugeInteger Web service is designed to perform calculations with integers that have a maximum of 100 digits. long variables cannot handle integers of this size (i.e., an overflow occurs). The Web service provides methods that take two “huge integers” (represented as strings) and determine their sum, their difference, which one is larger or smaller and whether the two numbers are equal.

You can think of these methods as services available to programmers of other applications via the Web (hence the term Web services). Any programmer can access this Web service, use the methods and thus avoid writing 172 lines of code.

Page 28: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

28

1. // HugeInteger.cs

2. // HugeInteger Web service performs operations on large integers.

3. using System;

4. using System.Web;

5. using System.Web.Services;

6. using System.Web.Services.Protocols;

7.

8. [ WebService( Namespace = "http://www.deitel.com/",

9. Description = "A Web service that provides methods for" +

10. " manipulating large integer values" ) ]

11. [ WebServiceBinding( ConformsTo = WsiProfiles.BasicProfile1_1 ) ]

12. public class HugeInteger : System.Web.Services.WebService

13. {

14. private const int MAXIMUM = 100; // maximum number of digits

15. public int[] number; // array representing the huge integer

16.

Page 29: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

29

17. // default constructor

18. public HugeInteger()

19. {

20. number = new int[ MAXIMUM ];

21. } // end default constructor

22.

23. // indexer that accepts an integer parameter

24. public int this[ int index ]

25. {

26. get

27. {

28. return number[ index ];

29. } // end get

30.

Page 30: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

30

31. set

32. {

33. number[ index ] = value;

34. } // end set

35. } // end indexer

36.

37. // returns string representation of HugeInteger

38. public override string ToString()

39. {

40. string returnString = "";

41.

42. foreach ( int i in number )

43. returnString = i + returnString;

44.

45. return returnString;

46. } // end method ToString

47.

Page 31: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

31

48. // creates HugeInteger based on argument

49. public static HugeInteger FromString( string value )

50. {

51. // create temporary HugeInteger to be returned by the method

52. HugeInteger parsedInteger = new HugeInteger();

53.

54. for ( int i = 0 ; i < value.Length; i++ )

55. parsedInteger[ i ] = Int32.Parse(

56. value[ value.Length - i - 1 ].ToString() );

57.

58. return parsedInteger;

59. } // end method FromString

60.

61. // WebMethod that adds integers represented by the String arguments

62. [ WebMethod( Description="Adds two huge integers." ) ]

Page 32: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

32

63. public string Add( string first, string second )

64. {

65. int carry = 0;

66. HugeInteger operand1 = HugeInteger.FromString( first );

67. HugeInteger operand2 = HugeInteger.FromString( second );

68. HugeInteger result = new HugeInteger(); // stores result of addition

69.

70. // perform addition algorithm for each digit

71. for ( int i = 0; i < MAXIMUM; i++ )

72. {

73. // add two digits in same column,

74. // result is their sum plus carry from

75. // previous operation, modulo 10

76. result[ i ] =

77. ( operand1[ i ] + operand2[ i ] + carry ) % 10;

78.

Page 33: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

33

79. // set carry to remainder of dividing sums of two digits by 10

80. carry = ( operand1[ i ] + operand2[ i ] + carry ) / 10;

81. } // end for

82.

83. return result.ToString();

84. } // end method Add

85.

86. // WebMethod that subtracts integers

87. // represented by the string arguments

88. [ WebMethod( Description="Subtracts two huge integers." ) ]

89. public string Subtract( string first, string second )

90. {

91. HugeInteger operand1 = HugeInteger.FromString( first );

92. HugeInteger operand2 = HugeInteger.FromString( second );

93. HugeInteger result = new HugeInteger();

94.

Page 34: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

34

95. // subtract bottom digit from top digit

96. for ( int i = 0; i < MAXIMUM; i++ )

97. {

98. // if top digit is smaller than bottom digit we need to borrow

99. if ( operand1[ i ] < operand2[ i ] )

100. Borrow( operand1, i );

101.

102. // subtract bottom from top

103. result[ i ] = operand1[ i ] - operand2[ i ];

104. } // end for

105.

106. return result.ToString();

107. } // end method Subtract

108.

Page 35: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

35

109. // borrow 1 from next digit

110. private void Borrow( HugeInteger hugeInteger, int place )

111. {

112. // if no place to borrow from, signal problem

113. if ( place >= MAXIMUM - 1 )

114. throw new ArgumentException();

115.

116. // otherwise if next digit is zero, borrow from column to left

117. else if ( hugeInteger[ place + 1 ] == 0 )

118. Borrow( hugeInteger, place + 1 );

119.

120. // add ten to current place because we borrowed and subtract

121. // one from previous digit--this is the digit we borrowed from

122. hugeInteger[ place ] += 10;

123. hugeInteger[ place + 1 ]--;

124. } // end method Borrow

125.

Page 36: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

36

126. // WebMethod that returns true if first integer is bigger than second

127. [ WebMethod( Description="Determines whether the first integer is " +

128. "larger than the second integer." ) ]

129. public bool Bigger( string first, string second )

130. {

131. char[] zeros = { '0' };

132.

133. try

134. {

135. // if elimination of all zeros from result

136. // of subtraction is an empty string,

137. // numbers are equal, so return false, otherwise return true

138. if ( Subtract( first, second ).Trim( zeros ) == "" )

139. return false;

140. else

141. return true;

142. } // end try

Page 37: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

37

143. // if ArgumentException occurs, first

144. // number was smaller, so return false

145. catch ( ArgumentException exception )

146. {

147. return false;

148. } // end catch

149. } // end method Bigger

150.

151. // WebMethod returns true if first integer is smaller than second

152. [ WebMethod( Description="Determines whether the first integer " +

153. "is smaller than the second integer." ) ]

154. public bool Smaller( string first, string second )

155. {

156. // if second is bigger than first, then first is smaller than second

157. return Bigger( second, first );

158. } // end method Smaller

159.

Page 38: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

38

160.// WebMethod that returns true if two integers are equal

161. [ WebMethod( Description="Determines whether the first integer " +

162. "is equal to the second integer." ) ]

163. public bool EqualTo( string first, string second )

164. {

165. // if either first is bigger than second, or first is

166. // smaller than second, they are not equal

167. if ( Bigger( first, second ) || Smaller( first, second ) )

168. return false;

169. else

170. return true;

171. } // end method EqualTo

172.} // end class HugeInteger

Page 39: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

39

Lines 8-10 contain a WebService attribute. Attaching this attribute to a Web service class declaration allows you to specify the Web service’s namespace and description. Like an XML namespace, a Web service’s namespace is used by client applications to differentiate that Web service from other available on the Web.

Line 8 assigns http://www.deitel.com as the Web service’s namespace using the WebService attribute’s Namespace property.

Lines 9-10 use the WebService attribute’s Description property to describe the Web service’s purpose – this appears in the ASMX page.

Visual Studio IDE places line 11 in all newly created Web services. This line indicates that the Web service confirms to the Basic Profile 1.1 (BP 1.1) developed by the Web Services Interoperability Organization (WS-1), a group dedicated to promoting interoperability among Web services developed on different platforms with different programming languages. BP 1.1 is a document that defines best practices for various aspects of Web service creation and consumption (www.WS-I.org).

Page 40: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

40

.NET environment hides many of these details from you. Setting the WebServiceBinding attribute’s ConformsTo property to WsiProfile.BasicProfile1_1 instructs Visual Studio IDE to perform its “behind-the-scenes” work, such as generating WSDL and ASMX files, in conformance with the guidelines laid out in BP 1.1.

By default, each new Web service class created in Visual studio IDE inherits from class System.Web.Services.WebService (line 12). Although a Web service need not derive from class WebService, this class provides members that are useful in determining information about the client and the Web service itself.

Several methods in class HugeInteger are tagged with the WebMethod attribute (lines 62, 88, 127, 152 and 161), which exposes a method so that it can be called remotely. When this attribute is absent, the method is not accessible to clients that consume the Web service.

Note that this attribute, like the WebService attribute, contains a Description property that allows the ASMX page to display information about the method.

Page 41: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

41

Lines 24-35 define an indexer, which enables us to access any digit in a HugeInteger.

Lines 62-84 and 88-107 define Web methods Add and Subtract, which perform addition and subtraction, respectively. Method Borrow (lines 110-124) handles the case in which the digit that we are currently examining in the left operand is smaller than the corresponding digit in the right operand. For instance, when we subtract 19 from 32, we usually examine the numbers in the operands digit-by-digit, starting from the right. The number 2 is smaller than 9, so we add 10 to 2 (resulting in 12). After borrowing, we can subtract 9 from 12, resulting in 3 for the rightmost digit in the solution. We then subtract 1 from the 3 in 32 – the next digit to the left (i.e., the digit we borrowed from). This leaves a 2 in the tens place. The corresponding digit in the other operand is now 1 in 19. subtracting 1 from 2 yields 1, making the corresponding digit in the result 1. The final result, when the digits are put together, is 13.

Method Borrow is the method that adds 10 to the appropriate digits and subtracts 1 from the digits to the left. This is a utility method that is not intended to be called remotely, so it is not qualified with attribute WebMethod.

Page 42: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

42

Building a Web Service in Visual Studio IDE

We now show how to create the HugeInteger Web service. In the following steps, you will create an ASP.NET Web Service project that executes on your computer’s local IIS Web server. To create the HugeInteger Web service in Visual Studio IDE, perform the following steps:

Page 43: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

43

Step1: Creating the Project To begin, we must create a project of type ASP.NET Web Service. Select File > New Web Site… to display the New Web Site dialog.

Select ASP.NET Web Service in the Templates pane. Select HTTP from the Location drop-down list to indicate that the files should be placed on a Web server. By default, Visual Studio IDE indicates that it will place the files on the local machine’s IIS Web server in a virtual directory named WebSite (http://localhost/WebSite).

Replace the name WebSite with HugeInteger for this example. Next, select Visual C# from the Language drop-down list to indicate

that you will use Visual C# to build the Web service. Visual Studio IDE places the Web service project’s solution file (.sln) in

the Projects subfolder within the current Windows user’s My Documents\Visual Studio 2008 folder. If you don’t have access to an IIS Web server to build ant test the examples, you can select File System from the Location drop-down list. In this case, IDE will place the Web service’s files on your local hard disk. You will be then be able to test the Web server using IDE’s built-in Web server.

Page 44: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

44

Page 45: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

45

Step2: Examining the Newly Created Project When the project is created, the code-behind file service.cs, which

contains code for a simple Web service is displayed by default.

Page 46: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

46

If the code-behind file is not open, it can be opened by double clicking the file in the App_Code directory listed in the Solution Explorer.

Visual Studio IDE includes four using declarations that are helpful for developing Web services (lines 1-4).

By default, a new code-behind file defines a class named Service that is marked with the WebService and WebServiceBinding attributes (lines 6-7).

The class contains a sample Web method named HelloWorld (lines 14-17). This method is a placeholder that you will replace with your own method(s).

Page 47: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

47

Step 3: Modifying and Renaming the Code-Behind File

To create the HugeInteger Web service, modify Service.cs by replacing all of the sample code provided by Visual Studio IDE with all of the code from the HugeInteger code-behind file. Then rename the HugeInteger.cs.

Page 48: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

48

Step 4: Examining the ASMX File

The Solution Explorer lists one file – Service.asmx – in addition to the code-behind file.

Recall that a Web service’s ASMX page, when accessed through a Web browser, displays information about the Web service’s methods and provides access to the Web service’s WSDL information.

However, if you open the ASMX file on disk, you will see that it actually contains only:

<%@ WebService Language=“C#” CodeBehind=“~/App_Code/Service.cs” Class=“Service” %>

to indicate the programming language in which the Web service’s code-behind file is written, the code-behind file’s location and the class that defines the Web service. When you will request the ASMX page through IIS, ASP.NET uses this information to generate the content displayed in the Web browser.

Page 49: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

49

Step 5: Modifying the ASMX File

Whenever you change the name of the code-behind file or the name of the class that defines the Web service, you must modify the ASMX file accordingly. Thus, after defining class HugeInteger in the code-behind file HugeInteger.cs, modify the ASMX file to contain the lines:

<%@ WebService Language=“C#” CodeBehind = “~/App_Code/HugeInteger.cs” Class = “HugeInteger” %>

Page 50: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

50

Step 6: Renaming the ASMX File

The final step in creating the HugeInteger Web service is to rename the ASMX file HugeInteger.asmx

Page 51: Web Services Dr. Awad Khalil Computer Science & Engineering Department AUC

Introduction to the Internet & WWW by Dr. Khalil

51

Deploying the HugeInteger Web Service

The Web service is already deployed because we created the HugeInteger Web service directly to our computer’s local IIS server.

You can choose Build Web Site from the Build menu to ensure that the Web service compiles without errors.

You can also test the Web service directly from Visual Studio IDE by selecting Start Without Debugging from the Debug menu. This opens a browser window that contains the following ASMX page. Clicking the link for a particular HugeInteger Web service method displays a Web page that allows you to test the method, as we have shown before.

Note that you can also access the Web service’s ASMX page from your computer by typing the following URL in a Web browser:

http://host/HugeInteger.asmx

where host is the hostname or IP address of the Web server.