slide1
Download
Skip this Video
Download Presentation
Web Services

Loading in 2 Seconds...

play fullscreen
1 / 82

Web Services - PowerPoint PPT Presentation


  • 89 Views
  • Uploaded on

22. Web Services. 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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Web Services' - kyna


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1
22
  • Web Services
objectives
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.
slide3
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
slide4
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
22 1 introduction
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
22 2 net web services basics
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
performance tip 22 1
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.
22 2 1 creating a web service in visual web developer
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
22 2 2 discovering web services
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
22 2 3 determining a web service s functionality
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
22 2 3 determining a web service s functionality cont
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
22 2 4 testing a web service s methods
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
error prevention tip 22 1
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.
22 2 5 building a client to use a web service
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
22 3 soap
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
slide23

Outline

HugeInteger.asmx

Placeholders

22 4 publishing and consuming web services
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
22 4 1 defining the hugeinteger web service
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
22 4 1 defining the hugeinteger web service cont
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
slide27

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

slide28

Outline

HugeInteger.cs

(2 of 7)

slide29

Outline

HugeInteger.cs

(3 of 7)

Allows the method to be called by the client

slide30

Outline

HugeInteger.cs

(4 of 7)

Allows the method to be called by the client

slide31

Outline

HugeInteger.cs

(5 of 7)

Allows the method to be called by the client

slide32

Outline

HugeInteger.cs

(6 of 7)

Allows the method to be called by the client

slide33

Outline

HugeInteger.cs

(7 of 7)

Allows the method to be called by the client

common programming error 22 1
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.
portability tip 22 1
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.
portability tip 22 2
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.
common programming error 22 2
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.
22 4 2 building a web service in visual web developer
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
22 4 3 deploying the hugeinteger web service
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
22 4 3 deploying the hugeinteger web service cont
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
error prevention tip 22 2
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.
22 4 4 creating a client to consume the hugeinteger web service
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)
22 4 4 creating a client to consume the hugeinteger web service cont
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)
slide51

Outline

UsingHugeIntegerService.cs

(1 of 8)

Declare variable of localhost.HugeInteger

slide52

Outline

The proxy object is created and assigned to remoteInteger

UsingHugeIntegerService.cs

(2 of 8)

Invoke method of the Web service

slide53

Outline

UsingHugeIntegerService.cs

(3 of 8)

Invoke method of the Web service

slide54

Outline

UsingHugeIntegerService.cs

(4 of 8)

Invoke method of the Web service

slide55

Outline

Invoke method of the Web service

UsingHugeIntegerService.cs

(5 of 8)

slide56

Outline

Invoke method of the Web service

UsingHugeIntegerService.cs

(6 of 8)

slide57

Outline

UsingHugeIntegerService.cs

(7 of 8)

slide58

Outline

UsingHugeIntegerService.cs

(8 of 8)

22 5 session tracking in web services
22.5 Session Tracking in Web Services
  • Session Tracking
    • Eliminates the need for information to be passed between the client and the Web service multiple times
      • Improves performance
    • Web methods can return personalized, localized results without requiring that the address be passed in each method call
      • After the user’s address is stored in a session variable
    • Requires less effort on the part of the programmer
      • Less information is passed in each method call
22 5 1 creating a blackjack web service
22.5.1 Creating a Blackjack Web Service
  • Web method’s EnableSession
    • Indicates that session information should be maintained and should be accessible to the method
    • Required only for methods that must access the session information
slide61

Outline

Assign the Web service’s namespace and description

BlackjackService.cs

(1 of 5)

Indicates that the Web service conforms to Basic Profile 1.1

Indicates that session information should be maintained and accessible to this method

Obtains the current user’s deck as an ArrayList from the Web service’s Session object

slide62

Outline

Indicates that session information should be maintained and accessible to this method

BlackjackService.cs

(2 of 5)

Adds the ArrayList to the Session object to maintain the deck between method calls from a particular client

slide63

Outline

BlackjackService.cs

(3 of 5)

Allows the method to be called by the client

slide64

Outline

BlackjackService.cs

(4 of 5)

slide65

Outline

BlackjackService.cs

(5 of 5)

22 5 2 consuming the blackjack web service
22.5.2 Consuming the Blackjack Web Service
  • CookieContainer
    • Namespace System.Net
    • Stores the information from a cookie in a Cookie object
    • Without creating a CookieContainer object, the Web service would create a new Session object for each request
      • The user’s state information would not persist across requests
  • Cookie
    • Contains a unique identifier that the Web service can use to recognize the client when the client makes future requests
    • The cookie is automatically sent back to the server as part of each request
slide67

Outline

Blackjack.cs

(1 of 16)

Create an instance of BlackjackService to represent dealer

slide68

Outline

Blackjack.cs

(2 of 16)

Instantiate the “dealer”

Create a new CookieContainer object for the dealer’s CookieContainer property

slide69

Outline

Blackjack.cs

(3 of 16)

slide70

Outline

Blackjack.cs

(4 of 16)

Invoke method of the Web service

slide71

Outline

Blackjack.cs

(5 of 16)

slide72

Outline

Blackjack.cs

(6 of 16)

slide73

Outline

Blackjack.cs

(7 of 16)

slide74

Outline

Invoke method of the Web service

Blackjack.cs

(8 of 16)

slide75

Outline

Blackjack.cs

(9 of 16)

Invoke method of the Web service

slide76

Outline

Blackjack.cs

(10 of 16)

Invoke method of the Web service

slide77

Outline

Blackjack.cs

(11 of 16)

slide78

Outline

a) Initial cards dealt to the player and the dealer when the user pressed theDeal button

Blackjack.cs

(12 of 16)

slide79

Outline

b) Cards after the player pressed the Hit button twice, then the Stay button. In this case, the player won the game with a higher total than the dealer.

Blackjack.cs

(13 of 16)

slide80

Outline

c) Cards after the player pressed the Hit button once, then the Stay button. In this case, the player busted (exceeded 21) and the dealer won the game.

Blackjack.cs

(14 of 16)

slide81

Outline

d) Cards after the player pressed the Deal button. In this case, the player won with Blackjack because the first two cards were an ace and a card with a value of 10 (a jack in this case.)

Blackjack.cs

(15 of 16)

slide82

Outline

e) Cards after the player pressed the Stay button. In this case, the player and dealer push—they have the same card total.

Blackjack.cs

(16 of 16)

ad