Web Services - PowerPoint PPT Presentation

slide1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Web Services PowerPoint Presentation
Download Presentation
Web Services

play fullscreen
1 / 82
Web Services
104 Views
Download Presentation
kyna
Download Presentation

Web Services

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 22 • Web Services

  2. OBJECTIVES In this chapter you will learn: • What a Web service is. • How to create Web services. • The important part that XML and the XML-based Simple Object Access Protocol play in enabling Web services. • The elements that comprise Web services, such as service descriptions and discovery files. • How to create a client that uses a Web service. • How to use Web services with Windows applications and Web applications. • How to use session tracking in Web services to maintain state information for the client.

  3. 22.1 Introduction • 22.2 .NET Web Services Basics • 22.2.1 Creating a Web Service in Visual Web Developer • 22.2.2 Discovering Web Services • 22.2.3 Determining a Web Service’s Functionality • 22.2.4 Testing a Web Service’s Methods • 22.2.5 Building a Client to Use a Web Service • 22.3 SOAP • 22.4 Publishing and Consuming Web Services • 22.4.1 Defining the HugeInteger Web Service • 22.4.2 Building a Web Service in Visual Web Developer • 22.4.3 Deploying the HugeInteger Web Service

  4. 22.4.4 Creating a Client to Consume the HugeInteger Web Service • 22.4.5  Consuming the HugeInteger Web Service • 22.5 Session Tracking in Web Services • 22.5.1 Creating a Blackjack Web Service • 22.5.2 Consuming the Blackjack Web Service

  5. 22.1 Introduction • Web Services • A class that allows its methods to be called by methods on other machines via common data formats and protocol • Ex: HTTP and XML • The over-the-network method calls are commonly implemented through the Simple Object Access Protocol (SOAP) • An XML-based protocol describes how to mark up requests and responses so that they can be transferred via protocols • Applications represent and transmit data in a standardized XML-based format using SOAP • Visual Web Developer and the .NET Framework provide a simple, user-friendly way to create Web services

  6. 22.2 .NET Web Services Basics • Basics • Remote machine • The machine on which the Web service resides • A Web service is typically implemented as a class • A compiled version of the Web service class is not placed in the current application’s directory • Requests to and responses from Web services created with Visual Web Developer are typically transmitted via SOAP • 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 • Web services and SOAP are platform and language independent

  7. Performance Tip 22.1 • 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.

  8. 22.2.1 Creating a Web Service in Visual Web Developer • Creating Web Services • Create a project of type ASP.NET Web Service. • Visual Web Developer then generates the following: • Files to contain the Web service code • Implements the Web service • An ASMX file • Provides access to the Web service • A DISCO file • Potential clients use to discover the Web service • Methods in a Web service are invoked through a Remote Procedure Call (RPC) • These methods are marked with the WebMethod attribute • Makes the method accessible to other classes through RPCs • Known as exposing

  9. Fig. 22.1| Web service components.

  10. 22.2.2 Discovering Web Services • Discovery of Web services (DISCO) • Microsoft-specific technology used to locate Web services on a server • Four types of DISCO files facilitate the discovery process: • .disco files • XML markup that describes for clients the location of Web services • Accessed via a Web service’s ASMX page • Contains references only to files in the current Web service • .vsdisco files • XML markup that describes the locations of Web services is generated dynamically, then returned to the client • Does not store the markup generated in response to a request • Contain only a small amount of data and provide up-to-date information about a server’s available Web services • Generate more overhead than .disco files do • A search must be performed every time a .vsdisco file is accessed • .discomap files • The resulting data that is returned from accessing a .disco file • .map files

  11. 22.2.3 Determining a Web Service’s Functionality • Web Service’s Functionality • Web services normally contain a service description • An XML document that conforms to Web Service Description Language (WSDL) • An XML vocabulary that defines the methods a Web service • Specifies lower-level information that clients might need

  12. 22.2.3 Determining a Web Service’s Functionality (Cont.) • ASMX File • Generated by the Visual Web Developer when a Web service is constructed • Presents Web method descriptions and links to test pages • Allow users to execute sample calls to these methods • Specifies the Web service’s : • Implementation class • Optional code-behind file • Where the Web service is defined • The assemblies • Referenced by the Web service • Accessed when the Web server receives a request for the Web service • Invokes the Web service implementation • For more technical information about the Web service, access the WSDL file • ASP.NET generates WSDL information dynamically • Ensures that clients receive the most current information about the Web service

  13. Link to the service description Links to the Web service’s methods Fig. 22.2| ASMX file rendered in a Web browser.

  14. 22.2.4 Testing a Web Service’s Methods • SOAP and HTTP POST • Two options for sending and receiving messages in Web services • Web service’s wire format or wire protocol • The protocol that transmits request-and-response messages • SOAP is the more commonly used wire format • SOAP messages can be sent using several transport protocols • 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

  15. Fig. 22.3| Service description for our HugeInteger Web service.

  16. Fig. 22.4| Invoking a Web method from a Web browser.

  17. Fig. 22.5| Results of invoking a Web method from a Web browser.

  18. Error-Prevention Tip 22.1 • Using the ASMX page of a Web service to test and debug methods can help you make the Web service more reliable and robust.

  19. 22.2.5 Building a Client to Use a Web Service • Client Using a Web Service • To consume a Web service, add a Web reference to the client • Adds files that allow the client to access the Web service • Visual C# 2005 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 • Visual C# 2005 provides an Update Web Reference option • The WSDL information is used to create a proxy class • Handles all the “plumbing” required for Web method calls • When the client application calls a Web method, it actually calls a method in the proxy class • This method has the same name and parameters as the Web method that is being called • 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 • The proxy class deserializes the SOAP message containing the response and returns the results of the Web method that was called

  20. Fig. 22.6| .NET Web service client after a Web reference has been added.

  21. 22.3 SOAP • SOAP • Each request and response is packaged in a SOAP message • XML message contains information that is required to process the message • The wire format used to transmit requests and responses must support all types passed between the applications • The request and all relevant information are packaged in a SOAP message and sent to the server on which the Web service resides • The Web service processes the contents which specify the method that the client wishes to execute and any arguments the client is passing to that method • Known as parsing a SOAP message • The proper method is then called with the specified arguments 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

  22. Fig. 22.7| Interaction between a Web service client and a Web service.

  23. Outline HugeInteger.asmx Placeholders

  24. 22.4 Publishing and Consuming Web Services • Terms • Publishing • Creating a Web services • Consuming • Using a Web services • Visual C# 2005 and Visual Web Developer create proxy classes for you

  25. 22.4.1 Defining the HugeInteger Web Service • WebService attribute • Allows you to specify the Web service’s namespace and description • Namespace property • The namespace that the Web service uses • Description property • Describe the Web service’s purpose • ConformsTo property • Instructs Visual Web Developer to perform its “behind-the-scenes” work in conformance with to a certain guideline • By default, each new Web service class created in Visual Web Developer inherits from class System.Web.Services.WebService

  26. 22.4.1 Defining the HugeInteger Web Service (Cont.) • WebMethod attribute • Exposes a method so it can be called remotely • When this attribute is absent, the method is not accessible to the clients

  27. Outline Namespaces for Web services HugeInteger.cs (1 of 7) Assign the Web service’s namespace and description Indicates that the Web service conforms to Basic Profile 1.1 Web service class inherits from System.Web.Services.WebService Enable access to any digit in a HugeInteger

  28. Outline HugeInteger.cs (2 of 7)

  29. Outline HugeInteger.cs (3 of 7) Allows the method to be called by the client

  30. Outline HugeInteger.cs (4 of 7) Allows the method to be called by the client

  31. Outline HugeInteger.cs (5 of 7) Allows the method to be called by the client

  32. Outline HugeInteger.cs (6 of 7) Allows the method to be called by the client

  33. Outline HugeInteger.cs (7 of 7) Allows the method to be called by the client

  34. Common Programming Error 22.1 • Failing to expose a method as a Web method by declaring it with the WebMethod attribute prevents clients of the Web service from accessing the method.

  35. Portability Tip 22.1 • Specify a namespace for each Web service so that it can be uniquely identified by clients. In general, you should use your company’s domain name as the Web service’s namespace, since company domain names are guaranteed to be unique.

  36. Portability Tip 22.2 • Specify descriptions for a Web service and its Web methods so that the Web service’s clients can view information about the service in the service’s ASMX page.

  37. Common Programming Error 22.2 • No method with the WebMethod attribute can be declared static—for a client to access a Web method, an instance of that Web service must exist.

  38. 22.4.2 Building a Web Service in Visual Web Developer • To create an ASP.NET Web Service project that executes on your computer’s local IIS Web server: • Step 1: Create the Project • Must be project of type ASP.NET Web Service • Step 2: Examine the Newly Created Project • The code-behind file Service.cs contains code for a simple Web service • Step 3: Modify and Rename the Code-Behind File • Step 4: Examine the ASMX File • Step 5: Modify the ASMX File • Step 6: Rename the ASMX File

  39. Fig. 22.10| Creating an ASP.NET Web Service in Visual Web Developer.

  40. Fig. 22.11| Code view of a Web service.

  41. 22.4.3 Deploying the HugeInteger Web Service • Accessing the HugeInteger Web Service’s ASMX Page from Another Computer • Access the Web service’s ASMX page from your computer by typing the following URL in a Web browser in the form: • http://localhost/HugeInteger/HugeInteger.asmx • By deploying the Web service on an IIS Web server, a client can connect to that server to access the Web service with a URL of the form: • http://host/HugeInteger/HugeInteger.asmx

  42. 22.4.3 Deploying the HugeInteger Web Service (Cont.) • Accessing the HugeInteger Web Service’s ASMX Page When the Web Service Executes in Visual Web Developer’s Built-in Web Server • Web servers typically receive requests on port 80 • Visual Web Developer’s Web server receives requests on a randomly selected port number • Ensure that Visual Web Developer’s built-in Web server does not conflict with another Web server running • Requests must be specify the port number as part of the request • Except when on port 80 • The URL to access the HugeInteger Web service’s ASMX page would be of the form: • http://host:portNumber/HugeInteger/HugeInteger.asmx

  43. Error-Prevention Tip 22.2 • Update the Web service’s ASMX file appropriately whenever the name of a Web service’s code-behind file or the class name changes. Visual Web Developer creates the ASMX file, but does not automatically update it when you make changes to other files in the project.

  44. 22.4.4 Creating a Client to Consume the HugeInteger Web Service • Create a Windows application as the client using Visual C# 2005: • Step 1: Open the Add Web Reference Dialog (Fig. 22.12) • Step 2: Locate Web Services on Your Computer (Fig. 22.13) • Step 3: Choose the Web Service to Reference (Fig. 22.14) • Step 4: Add the Web Reference (Fig. 22.15) • Step 5: View the Web Reference in the Solution Explorer (Fig. 22.16)

  45. Fig. 22.12| Adding a Web service reference to a project.

  46. Fig. 22.13| Add Web Referencedialog.

  47. Fig. 22.14| Web services located on localhost.

  48. Fig. 22.15| Web reference selection and description.

  49. Fig. 22.16| Solution Explorer after adding a Web reference to a project.

  50. 22.4.4 Creating a Client to Consume the HugeInteger Web Service (Cont.) • Notes on Creating a Client to Consume a Web Service • Add the Web reference first • So Visual C# 2005 can recognize an object of the Web service proxy class • Can access the Web service through an object of the proxy class • The proxy class is located in namespace localhost, so you must use localhost.class_name to reference this class • Must create an object of the proxy class to access the Web service • Do not need access to the proxy class’s code • Can invoke the proxy object’s methods as if it were an object of the Web service class • Two common technologies facilitate to locating a new Web service • Universal Description, Discovery and Integration (UDDI) • Discovery of Web services (DISCO)