1 / 51

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

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

fai
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Introduction to the Internet & WWW by Dr. Khalil Introduction • A webservice 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 SimpleObjectAccessProtocol (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.

  3. Introduction to the Internet & WWW by Dr. Khalil • 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.

  4. Introduction to the Internet & WWW by Dr. Khalil • 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.

  5. Introduction to the Internet & WWW by Dr. Khalil 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.

  6. Introduction to the Internet & WWW by Dr. Khalil .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.

  7. Introduction to the Internet & WWW by Dr. Khalil • 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.

  8. Introduction to the Internet & WWW by Dr. Khalil Creating a Web Service • To create a Web service in Visual Studio IDE, you first create a project of type ASP.NETWebService. 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.

  9. Introduction to the Internet & WWW by Dr. Khalil 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.

  10. Introduction to the Internet & WWW by Dr. Khalil • 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.

  11. Introduction to the Internet & WWW by Dr. Khalil • 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.

  12. Introduction to the Internet & WWW by Dr. Khalil 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 WebServiceDescriptionLanguage (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).

  13. Introduction to the Internet & WWW by Dr. Khalil • 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.

  14. Introduction to the Internet & WWW by Dr. Khalil

  15. Introduction to the Internet & WWW by Dr. Khalil • 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 ServiceDescription. 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 ?WSDLto the ASMX file’s URL or by clicking the ServiceDescription 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 ServiceDescription link at the top of the ASMX page, the browser displays the generated WSDL document containing the service description.

  16. Introduction to the Internet & WWW by Dr. Khalil

  17. Introduction to the Internet & WWW by Dr. Khalil Testing a Web Service’s Methods • Below the ServiceDescription 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 HTTPPOST. 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 wireprotocol, 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 HTTPPOST must use HTTP. When you test a Web service via an ASMX page, the ASMX page uses HTTPPOST 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.

  18. Introduction to the Internet & WWW by Dr. Khalil • 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.

  19. Introduction to the Internet & WWW by Dr. Khalil

  20. Introduction to the Internet & WWW by Dr. Khalil 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 addingaWebreference 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 SolutionExplorer and select AddWebReference…. 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 ShowAllFiles. • 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.

  21. Introduction to the Internet & WWW by Dr. Khalil • The WSDL information is used to create a proxyclass, 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.

  22. Introduction to the Internet & WWW by Dr. Khalil Simple Object Access Protocol (SOAP) • The SimpleObjectAccessProtocol (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.

  23. Introduction to the Internet & WWW by Dr. Khalil • 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 SOAPenvelope), 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 parsingaSOAPmessage. 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

  24. Introduction to the Internet & WWW by Dr. Khalil • 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.

  25. Introduction to the Internet & WWW by Dr. Khalil • POST /HugeInteger/HugeInteger.asmx HTTP/1.1 • Host: localhost • Content-Type: text/xml; charset=utf-8 • Content-Length: length • SOAPAction: http://deitel.com/Bigger • <?xml version=“1.0” encoding=“utf-8” ?> • <soap:Envelope • xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” • xmlns:xsd=http://www.w3.org/2001/XMLSchema • xmlns:soap=http://schemas.xmlsoap.org/soap/envelope/> • <soap:body> • <Bigger xmlns=“http://www.deitel.com”> • <first>string</first> • <second>string</second> • </Bigger> • <soap:body> • </soap:Envelope>

  26. Introduction to the Internet & WWW by Dr. Khalil 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.

  27. Introduction to the Internet & WWW by Dr. Khalil 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.

  28. Introduction to the Internet & WWW by Dr. Khalil • // HugeInteger.cs • // HugeInteger Web service performs operations on large integers. • using System; • using System.Web; • using System.Web.Services; • using System.Web.Services.Protocols; • [ WebService( Namespace = "http://www.deitel.com/", • Description = "A Web service that provides methods for" + • " manipulating large integer values" ) ] • [ WebServiceBinding( ConformsTo = WsiProfiles.BasicProfile1_1 ) ] • public class HugeInteger : System.Web.Services.WebService • { • private const int MAXIMUM = 100; // maximum number of digits • public int[] number; // array representing the huge integer

  29. Introduction to the Internet & WWW by Dr. Khalil • // default constructor • public HugeInteger() • { • number = new int[ MAXIMUM ]; • } // end default constructor • // indexer that accepts an integer parameter • public int this[ int index ] • { • get • { • return number[ index ]; • } // end get

  30. Introduction to the Internet & WWW by Dr. Khalil • set • { • number[ index ] = value; • } // end set • } // end indexer • // returns string representation of HugeInteger • public override string ToString() • { • string returnString = ""; • foreach ( int i in number ) • returnString = i + returnString; • return returnString; • } // end method ToString

  31. Introduction to the Internet & WWW by Dr. Khalil • // creates HugeInteger based on argument • public static HugeInteger FromString( string value ) • { • // create temporary HugeInteger to be returned by the method • HugeInteger parsedInteger = new HugeInteger(); • for ( int i = 0 ; i < value.Length; i++ ) • parsedInteger[ i ] = Int32.Parse( • value[ value.Length - i - 1 ].ToString() ); • return parsedInteger; • } // end method FromString • // WebMethod that adds integers represented by the String arguments • [ WebMethod( Description="Adds two huge integers." ) ]

  32. Introduction to the Internet & WWW by Dr. Khalil • public string Add( string first, string second ) • { • int carry = 0; • HugeInteger operand1 = HugeInteger.FromString( first ); • HugeInteger operand2 = HugeInteger.FromString( second ); • HugeInteger result = new HugeInteger(); // stores result of addition • // perform addition algorithm for each digit • for ( int i = 0; i < MAXIMUM; i++ ) • { • // add two digits in same column, • // result is their sum plus carry from • // previous operation, modulo 10 • result[ i ] = • ( operand1[ i ] + operand2[ i ] + carry ) % 10;

  33. Introduction to the Internet & WWW by Dr. Khalil • // set carry to remainder of dividing sums of two digits by 10 • carry = ( operand1[ i ] + operand2[ i ] + carry ) / 10; • } // end for • return result.ToString(); • } // end method Add • // WebMethod that subtracts integers • // represented by the string arguments • [ WebMethod( Description="Subtracts two huge integers." ) ] • public string Subtract( string first, string second ) • { • HugeInteger operand1 = HugeInteger.FromString( first ); • HugeInteger operand2 = HugeInteger.FromString( second ); • HugeInteger result = new HugeInteger();

  34. Introduction to the Internet & WWW by Dr. Khalil • // subtract bottom digit from top digit • for ( int i = 0; i < MAXIMUM; i++ ) • { • // if top digit is smaller than bottom digit we need to borrow • if ( operand1[ i ] < operand2[ i ] ) • Borrow( operand1, i ); • // subtract bottom from top • result[ i ] = operand1[ i ] - operand2[ i ]; • } // end for • return result.ToString(); • } // end method Subtract

  35. Introduction to the Internet & WWW by Dr. Khalil • // borrow 1 from next digit • private void Borrow( HugeInteger hugeInteger, int place ) • { • // if no place to borrow from, signal problem • if ( place >= MAXIMUM - 1 ) • throw new ArgumentException(); • // otherwise if next digit is zero, borrow from column to left • else if ( hugeInteger[ place + 1 ] == 0 ) • Borrow( hugeInteger, place + 1 ); • // add ten to current place because we borrowed and subtract • // one from previous digit--this is the digit we borrowed from • hugeInteger[ place ] += 10; • hugeInteger[ place + 1 ]--; • } // end method Borrow

  36. Introduction to the Internet & WWW by Dr. Khalil • // WebMethod that returns true if first integer is bigger than second • [ WebMethod( Description="Determines whether the first integer is " + • "larger than the second integer." ) ] • public bool Bigger( string first, string second ) • { • char[] zeros = { '0' }; • try • { • // if elimination of all zeros from result • // of subtraction is an empty string, • // numbers are equal, so return false, otherwise return true • if ( Subtract( first, second ).Trim( zeros ) == "" ) • return false; • else • return true; • } // end try

  37. Introduction to the Internet & WWW by Dr. Khalil • // if ArgumentException occurs, first • // number was smaller, so return false • catch ( ArgumentException exception ) • { • return false; • } // end catch • } // end method Bigger • // WebMethod returns true if first integer is smaller than second • [ WebMethod( Description="Determines whether the first integer " + • "is smaller than the second integer." ) ] • public bool Smaller( string first, string second ) • { • // if second is bigger than first, then first is smaller than second • return Bigger( second, first ); • } // end method Smaller

  38. Introduction to the Internet & WWW by Dr. Khalil • // WebMethod that returns true if two integers are equal • [ WebMethod( Description="Determines whether the first integer " + • "is equal to the second integer." ) ] • public bool EqualTo( string first, string second ) • { • // if either first is bigger than second, or first is • // smaller than second, they are not equal • if ( Bigger( first, second ) || Smaller( first, second ) ) • return false; • else • return true; • } // end method EqualTo • } // end class HugeInteger

  39. Introduction to the Internet & WWW by Dr. Khalil • 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 WebServicesInteroperabilityOrganization (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).

  40. Introduction to the Internet & WWW by Dr. Khalil • .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.

  41. Introduction to the Internet & WWW by Dr. Khalil • 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.

  42. Introduction to the Internet & WWW by Dr. Khalil 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.NETWebService 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:

  43. Introduction to the Internet & WWW by Dr. Khalil 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 NewWebSite dialog. Select ASP.NETWebService 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 IISWebserver in a virtual directory named WebSite (http://localhost/WebSite). • Replace the name WebSite with HugeInteger for this example. • Next, select VisualC# 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 MyDocuments\VisualStudio2008 folder. If you don’t have access to an IISWebserver to build ant test the examples, you can select FileSystem 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.

  44. Introduction to the Internet & WWW by Dr. Khalil

  45. Introduction to the Internet & WWW by Dr. Khalil 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.

  46. Introduction to the Internet & WWW by Dr. Khalil • 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 SolutionExplorer. • 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).

  47. Introduction to the Internet & WWW by Dr. Khalil 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.

  48. Introduction to the Internet & WWW by Dr. Khalil Step 4: Examining the ASMX File • The SolutionExplorer 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.

  49. Introduction to the Internet & WWW by Dr. Khalil 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” %>

  50. Introduction to the Internet & WWW by Dr. Khalil Step 6: Renaming the ASMX File • The final step in creating the HugeInteger Web service is to rename the ASMX file HugeInteger.asmx

More Related