Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Pearson Education, Inc. All rights reserved.

Similar presentations


Presentation on theme: " Pearson Education, Inc. All rights reserved."— Presentation transcript:

1  2009 - 2014 Pearson Education, Inc. All rights reserved.
Chapter 30 Web Services  Pearson Education, Inc. All rights reserved. contributed by J. Goetz

2 A client is to me a mere unit, a factor in a problem.
Sir Arthur Conan Doyle ...if the simplest things of nature have a message that you understand, rejoice, for your soul is alive. Eleonora Duse Protocol is everything. Francoise Giuliani They also serve who only stand and wait. John Milton

3 OBJECTIVES In this chapter you will learn:
What a WCF Windows Communication Foundation service is. How to create WCF web services. How XML, JSON, XML-Based Simple Object Access Protocol (SOAP) and Representational State Transfer (REST) Architecture enable WCF web services. The elements that comprise WCF web services, such as service references, service endpoints, service contracts and service bindings.

4 OBJECTIVES How to create a client that consumes a WCF web service.
How to use WCF web services with Windows applications and web applications. How to use session tracking in WCF web services to maintain state information for the client. How to pass user-defined types to a WCF web service. 30.1   Introduction 30.2   WCF Services Basics 30.3   HTTP get and post request 30.4   Representational State Transfer (REST) 30.5   JavaScript Object Notation (JSON) 30.6   Publishing and Consuming SOAP-Based Web Services 30.6   Publishing and Consuming REST-Based XML Web Services 30.7   Publishing and Consuming REST-Based JSON Web Services 30.8  Equation Generator: Returning User- Defined Types

5 Web Services DLLs facilities software reusability and modularity on the same machine WEB Services promotes reusability in distributed systems A Web service is a group of network-accessible operations (applications) that other systems can invoke over the Internet

6 Common Language Specification
Web Services VB C++ C# JScript J# Visual Studio.NET Common Language Specification ASP.NET Web Forms Web Services Mobile Internet Toolkit Windows Forms ADO.NET and XML Base Class Library Common Language Runtime Operating System The Framework Class Library (FCL) includes classes for creating Web Services Which are applications packaged as services that client can access via the Internet Any C# application is a potential Web service, so C# programmers can reuse existing applications as building blocks to form larger more sophisticated Web-enabled applications.

7 30.1  Introduction Windows Communication Foundation (WCF) services are a set of technologies for communicating over networks. WCF uses a common framework for all communication, so you need to learn only one programming model. A web service is a class that allows its methods to be called by methods on other machines via common data formats and protocols.

8 30.1  Introduction (Cont.) In .NET, method calls are commonly implemented through Simple Object Access Protocol (SOAP) or Representational State Transfer (REST). SOAP is an XML-based protocol of requests and responses. REST uses the web’s traditional request/response mechanisms such as GET and POST requests.

9 In SOAP Web service uses XML and other specific technical standards SOAP, WSDL and UDDI (Universal Description, Discovery and Integration). These technologies enable communication among applications in a manner that is independent of specific programming languages, operating systems and hardware platforms. XML – allows exchange of data between any kind of device regardless of OS or language it runs on SOAP (Simple Object Access Protocol) provides a communication mechanism between Web services and applications, WSDL (Web Service Description Language) offers a uniform method of describing Web services to other programs and UDDI provides a directory for registering and searching web services enables the creation of searchable Web services directories. When deployed together, these technologies allow developers to package applications as services and publish those services on the Web.

10 Web Service Description Language
WSDL is an XML-based language. When a web service is created using Visual Studio.NET, a WSDL document is also created for it. WSDL document tells: what methods a web service has, and their parameter types.

11 Web Services

12 Web Services Life-Cycle

13 Universal Description, Discovery and Integration - UDDI
A free search engine for existing web services ( A central registry for web services. - company can advertise its service by registering with UDDI. Contains APIs for programmatically locating information about web services. UDDI Structure

14 Client/Web Service Scenario
1: Discovery Consumer Client queries a UDDI directory over HTTP for a Location of a Web Service. Receives DISCO information Web Service

15 Client/Web Service Scenario
2: Service Description Consumer Client queries the Web service via HTTP for the WSDL Web Service

16 Client/Web Service Scenario
3: Method Description Consumer Client uses WSDL information to dynamically determine interfaces and return types Web Service

17 Client/Web Service Scenario
4: Method Call Consumer Client makes XML/ SOAP-encapsulated message call to the Web service, conforming to WSDL information Web Service

18 Security with Web Services HTTP and Firewalls
DCOM Service U Web Service Client Port 80 U CORBA Service Firewall

19 Web Services Application Model
Wireless Devices ASP.NET Applications Web Service 2 Browsers Windows Applications Web Service 3 Internet Boundary

20 Functional Web Service Flow
Requests to and responses from a Web-service method are packaged by default in a SOAP message - an XML message containing all the information necessary to process its contents. SOAP allows Web services to employ a variety of data types, including user-defined data types. When a program invokes a Web-service method, the request (method name with parameters) are packaged in a SOAP message and sent to the appropriate destination.

21 How XML Web Services Work
When the Web service receives the SOAP message, it processes the message's contents, which specifies the method that the client wishes to execute and the arguments the client is passing to that method. When the Web service receives a request, the request is parsed, and the proper method is called with the specified arguments (if there are any). The response is sent back to the client as another SOAP message. A Web Service is a Request/Response mechanism Windows 2010 Server Workstation Application Server SOAP message Request Request .NET Web Service HTTP protocol HTTP protocol Response Response HTML SOAP message SOAP-based services send and receive messages over HTTP connections.

22 Example of SOAP Communication
XML (SOAP) Request <?xml version=“1.0” encoding=“utf-8”?> <soap:Envelope xmlns:xsi=“ <soap:Body> <AuthorizeCreditCard xmlns=“ <CardType>Visa</CardType> <CardNumber> </C <ExpDate>012104</ExpDate> <Amount>21.86</Amount> <Currency>USD</Currency> </soap:Body> </soap:Envelope> HTTP Protocol Application Server Web Service Server XML (SOAP) Response <?xml version=“1.0” encoding=“utf-8”?> <soap:Envelope xmlns:xsi=“ <soap:Body> <AuthorizeCreditCard xmlns=“ <CardNumber> </C <Amount>21.86</Amount> <Currency>USD</Currency> <AuthCode> </AuthCode> </soap:Body> </soap:Envelope> HTTP Protocol XML is the language and SOAP is the format.

23 30.2  WCF Services Basics Microsoft’s Windows Communication Foundation (WCF) encompasses several existing technologies. Each WCF service can have 1 or more endpoints. An endpoint is the path through which a WCF client connects to a WCF service. Every endpoint has three key components: An address represents the service’s location. A binding specifies how a client communicates with the service. A contract is an interface representing the service’s methods and their return types. The machine on which the web service resides is the web service host. The client application sends a method call over a network to the web service host, which processes the call and returns a response.

24 30.3 Simple Object Access Protocol (SOAP)
Simple Object Access Protocol (SOAP) is a platform-independent communication protocol. A specification defining the XML format for messages that are passed to and from web services. SOAP messages are contain information in XML Limited to XML format only SOAP-based services send and receive messages over HTTP connections.

25 30.3 Simple Object Access Protocol (SOAP) (Cont.)
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 as DateTime, XmlNode­ and others. SOAP can also transmit arrays of these types.

26 30.3 Simple Object Access Protocol (SOAP) (Cont.)
When a program invokes a method of a SOAP web service, the request is packaged in a SOAP message, enclosed in a SOAP envelope and sent to the server. The web service parses the XML, then processes the message’s contents. The web service sends the response back to the client in another SOAP message.

27 30.4 Representational State Transfer (REST)
Representational State Transfer (REST) is an architectural style for implementing web services aws.amazon.com Uses a variety of formats (return data in): XML, JSON, HTML, plain text and media files Each operation in a RESTful web service is identified by a unique URL. The results of a particular operation may be cached locally by the browser. Unlike SOAP-based web services the request is not packaged in an envelope

28 30.5 JavaScript Object Notation (JSON)
JavaScript Object Notation (JSON) is an alternative to XML. JSON represents objects as collections of name/value pairs represented as Strings. JSON is a simple format that makes objects easy to read, create and parse: { propertyName1 : value1, propertyName2 : value2 }

29 30.5 JavaScript Object Notation (JSON) (Cont.)
Arrays are represented in JSON with square brackets: [ value1, value2, value3 ] {property1: value1, property2: value2, … } - object format To appreciate the simplicity of JSON, examine this array of address-book entries: [ { first: 'Cheryl', last: 'Black' }, { first: 'James', last: 'Blue' }, { first: 'Mike', last: 'Brown' }, { first: 'Meg', last: 'Gold' } ] Each value in an array can be a string number JSON object true false null

30 From Resa/Microsoft WF7 Irvine
Simple Test Case : Downloaded 8 data records – Each Record had just four fields • Measured bytes to transfer: – Odata version from WCF Data Services • 8.5k – ASMX SOAP + Dataset - 3k – REST + XML - 1.2k – REST + JSON – 639bytes So 30.6 Publishing and Consuming SOAP-Based WCF Web Services is updated to 30.6 Publishing and Consuming REST-Based XML Web Services

31 30.6 Publishing and Consuming REST-Based XML Web Services
30.6.1 Creating a WCF Web Service To build a SOAP-based web service in Visual Web Developer, create a WCF Service project. SOAP is the default protocol for WCF web services. Visual Web Developer generates files for the WCF service code, an SVC file (Service.svc), and a Web.config file.

32 30.6.1 Creating a REST-Based XML WCF Web Service
Create a new WCF Service project. IWelcomeRESTXMLService interface (Fig. 30.11) is a modified version of the IWelcomeSOAPXMLService interface. Outline IWelcomeRESTXML Service.cs This namespace is imported to use web service attributes. To use WebGet attributes The set of methods and properties the client uses to access the service. The ServiceContract attribute exposes a class that implements the interface as a WCF web service. The OperationContract attribute exposes a method to clients for remote calls. The WebGet attribute maps a method­ to a unique URL. UriTemplate specify the format that is used to invoke the method. Fig | WCF web-service interface. A class that implements this interface returns a welcome message through REST architecture and XML data format.

33 Outline WelcomeRESTXMLService (Fig. 30.12) is the class that implements the IWelcomeREST­XMLService interface; it is similar to the WelcomeSOAPXMLService class (Fig. 30.2). WelcomeRESTXML Service.cs The string object will be serialized (converted) to XML, then returned to the client Fig | WCF web service that returns a welcome message using REST architecture and XML data format.

34 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)
30.6.3 Building a SOAP-Based Web Service Select File > New Web Site… to display the New Web Site dialog (Fig. 30.3). Ensure that you are making a WCF Service in Visual C#. Select File System from the Location drop-down list and set the location to WelcomeSOAPXMLService, then click OK.

35 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)

36 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)
Replace IService.cs and Service.cs in the App_Code directory with the code from IWelcomeSOAPXMLService and WelcomeSOAPXMLService. Rename the files accordingly.

37 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)
If you open the Service.svc file on disk, note that it contains only the following directives: ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %> When you request the SVC page in a web browser, WCF dynamically generates additional information about the web service. Modify the SVC file to reference the new code files: ServiceHost Language="C#" Debug="true" Service="WelcomeSOAPXMLService" CodeBehind="~/App_Code/WelcomeSOAPXMLService.cs" %>

38 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)
30.6.4 Deploying the WelcomeSOAPXMLService Right click the .svc file in Solution Explorer and select Set As Start Page. Next, choose Build Web Site from the Build menu. Once the service is running, you can access the SVC page from the URL shown in the web browser (Fig. 30.4):

39 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)
Fig | SVC file rendered in a web browser.

40 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)
By default, the ASP.NET Development Server assigns a random port number to each website it hosts. Click on the project name in the Solution Explorer. Set the Use dynamic ports property to False and set the Port number property to the unused TCP port that you want to use (Fig. 30.5).

41 30.6 Publishing and Consuming REST-Based XML Services (Cont.)
Fig. | WCF web service Properties window.

42 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)
A service description is an XML document that conforms to the Web Service Description Language (WSDL). WSDL is an XML vocabulary that defines the methods that a web service makes available. The WSDL document also specifies lower-level information that client might need , such as the required formats for requests and responses When viewed in a web browser, an SVC file presents a link to the service’s WSDL document.

43 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)
30.6.5 Creating a Client to Consume the WelcomeSOAPXMLService A .NET web-service client can be any type of .NET application: Win or web apps, console, any app written in different language on different platform

44 30.6 Publishing and Consuming REST-Based XML Services (Cont.)
You can enable a client application to consume a web service by adding a service reference to the client. A proxy class represents the web service. The client application accesses the web service via an instance of the proxy class. Fig. | .NET WCF web service client after a web-service reference has been added.

45 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)
Fig. | Interaction between a web-service client and a SOAP web service. A proxy class handles all the “plumbing” required for service method calls The networking details and Formation of SOAP messages

46 30.6 Publishing and Consuming REST-Based XML Web Services (Cont.)
Create an application in Visual C# 2012 named WelcomeSOAPXMLClient. Right click the project name in the Solution Explorer and select Add Service Reference… Enter the URL of WelcomeSOAPXMLService’s .svc file in the Address field. Change the Namespace field to ServiceReference. Click the Ok button. The Solution Explorer should now contain a Service References folder.

47 30.6.1 Creating a REST-Based XML WCF Web Service
Create a new WCF Service project. IWelcomeRESTXMLService interface (Fig. 30.11) is a modified version of the IWelcomeSOAPXMLService interface. Outline IWelcomeRESTXML Service.cs This namespace is imported to use web service attributes. To use WebGet attributes The set of methods and properties the client uses to access the service. The ServiceContract attribute exposes a class that implements the interface as a WCF web service. The OperationContract attribute exposes a method to clients for remote calls. The WebGet attribute maps a method­ to a unique URL. Fig | WCF web-service interface. A class that implements this interface returns a welcome message through REST architecture and XML data format.

48 30.6 Publishing and Consuming REST-Based XML WCF Web Services
The WebGet attribute maps a method to a unique URL (uniform resource locator). WebGet’s UriTemplate property specifies the URI (uniform resource identifier) format that is used to invoke the method.

49 Outline WelcomeRESTXMLService (Fig. 30.12) is the class that implements the IWelcomeREST­XMLService interface; it is similar to the WelcomeSOAPXMLService class (Fig. 30.2). WelcomeRESTXML Service.cs The string object will be serialized (converted) to XML Fig | WCF web service that returns a welcome message using REST architecture and XML data format.

50 Figure 30. 4 shows part of the default Web
Figure 30.4 shows part of the default Web.config file modified to use REST architecture. Outline ( 1 of 2 ) The behaviorConfiguration defines the endpoint’s behavior. Defining RESTBehavior, and specifying that clients communicate using HTTP.

51 ( 2 of 2 ) Outline webHttpBinding is used to respond to REST-based requests.

52 30.6 Publishing and Consuming REST-Based XML WCF Web Services (Cont.)
Figure 30.12 tests the WelcomeRESTXMLService’s Welcome method in a web browser by following the URI template. The browser displays the XML data response from WelcomeRESTXMLService. Fig | Response from WelcomeRESTXMLService in XML data format.

53 Outline WelcomeRESTXMLForm (Fig. 30.7) invokes the web service and receive its response. WelcomeRESTXML Form.cs ( 1 of 4 ) Using the System.Net namespace’s HttpClient Fig | Client that consumes the WelcomeRESTXMLService. (Part 1 of 2.)

54 WelcomeRESTXML Form.cs
The client’s DownloadStringAsync method invokes the web service asynchronously. Parsing the XML response. We use the parameter to get the returned XML document. Fig | Client that consumes the WelcomeRESTXMLService. (Part 2 of 2.)

55 30.7 Publishing and Consuming REST-Based JSON WCF Web Services
Outline In Fig. 30.8, we modify the Welcome­RESTXMLService to return data in JSON format. IWelcomeRESTJSON Service.cs ( 1 of 2 ) Set the ResponseFormat property to WebMessageFormat.Json. Fig | WCF web-service interface that returns a welcome message through REST architecture and JSON format. (Part 1 of 2.)

56 Outline Only DataMembers of DataContract are serialized, so we need: IWelcomeRESTJSON Service.cs ( 2 of 2 ) The DataContract attribute exposes the TextMessage class for serialization. The DataMember attribute exposes a property of this class for serialization. Fig | WCF web-service interface that returns a welcome message through REST architecture and JSON format. (Part 2 of 2.) For JSON serialization to work properly, the objects being converted to JSON must have public properties. strings do not have public properties, so a serializable TextMessage class was used in this example.

57 Figure 30.9 shows the implementation of the interface of Fig. 30.8.
Outline WelcomeRESTJSON Service.cs The Welcome method returns a TextMessage object, automatically serialized in JSON format. Fig | WCF web service that returns a welcome message through REST architecture and JSON format.

58 30.7 Publishing and Consuming REST-Based JSON WCF Web Services (Cont.)
Test the web service by accessing the Service.svc file: Service/Service.svc Append the URI template ­(welcome/yourName) to the address. The service response is a JSON object (Fig. 30.10). Fig | Response from WelcomeRESTJSONService in JSON data format.

59 In Fig. 30.11, we consume the JSON web service.
Outline Custom types that are sent to or from a REST web service are converted using XML or JSON serialization. In Fig. 30.11, we consume the JSON web service. Right click the project name, select Add Reference and add System.ServiceModel.Web and System.Runtime.Serialization. WelcomeRESTJSON Form.cs ( 1 of 5 )

60 Outline WelcomeRESTJSON Form.cs
Fig | Client that consumes WelcomeRESTJSONService. (Part 1 of 2.)

61 Fig. 30.11 | Client that consumes WelcomeRESTJSONService.
WelcomeRESTJSON Form.cs ( 1 of 2 ) Creating an object for performing JSON serialization on TextMessage objects. Using the GetBytes method to convert the JSON response to a stream and deserialized into a TextMessage object Fig | Client that consumes WelcomeRESTJSONService.

62 Outline WelcomeRESTJSON Form.cs ( 5 of 5 )
The TextMessage class maps fields for JSON serialization. WelcomeRESTJSON Form.cs ( 5 of 5 ) a) User inputs name. b) Message sent from WelcomeRESTJSONService. Fig | Client that consumes WelcomeRESTJSONService. (Part 4 of 4.)

63 30.8 Equation Generator: Returning User-Defined Types
This section presents an EquationGenerator web service. The service generates random arithmetic equations of type Equation. The client uses user-inputted information to request an equation. The difficulty level is a string because variables for UriTemplate path segments must be of type string.

64 Outline Equation.cs ( 1 of 5 )
Fig | Class Equation that contains information about an equation. (Part 1 of 5.)

65 The parameterless constructor calls the three-argument constructor with default values.
Equation.cs ( 2 of 5 ) This constructor takes the left and right operands and the arithmetic operation as arguments. Fig | Class Equation that contains information about an equation. (Part 2 of 5.)

66 Equation.cs ( 3 of 5 ) This constructor takes the left and right operands and the arithmetic operation as arguments. Fig | Class Equation that contains information about an equation. (Part 3 of 5.)

67 Equation.cs ( 4,5 of 5 ) Fig | Class Equation that contains information about an equation. (Part 4,5 of 5.)

68 Modify the Web.config file to enable REST support as well.
Outline Figures 30.13–30.14 present the EquationGenerator­Service’s interface and class for creating randomly generated Equations. Modify the Web.config file to enable REST support as well. IEquation GeneratorService .cs Defining the REST request for an equation with a certain operation and difficulty level. Fig | WCF REST service interface to create random equations based on a specified operation and difficulty level.

69 EquationGenerator Service.cs
GenerateEquation’s parameters represent the mathematical operation and the difficulty level. The Equation is automatically serialized as an XML response. Fig | WCF REST service to create random equations based on a specified operation and difficulty level.

70 Outline The MathTutor application (Fig. 30.15) uses the web service to create its Equation objects. MathTutorForm.cs ( 1 of 8 ) Defining the WebClient that is used to invoke the web service. Fig | Math tutor using EquationGeneratorServiceXML to create equations. (Part 1 of 8.)

71 MathTutorForm.cs MathTutorForm.cs ( 2, 3 of 8 )
Invoking the EquationGeneratorService asynchronously. the XML response and displays the equation. Fig | Math tutor using EquationGeneratorServiceXML to create equations. (Part 2 of 8.)

72 Outline MathTutorForm.cs ( 4 of 8 )
Checking whether the user provided the correct answer. Fig | Math tutor using EquationGeneratorServiceXML to create equations. (Part 4 of 8.)

73 Outline MathTutorForm.cs ( 5 of 8 )
Fig | Math tutor using EquationGeneratorServiceXML to create equations. (Part 5 of 8.)

74 Outline MathTutorForm.cs ( 6 of 8 )
Fig | Math tutor using EquationGeneratorServiceXML to create equations. (Part 6 of 8.)

75 Outline MathTutorForm.cs ( 7 of 8 )
Fig | Math tutor using EquationGeneratorServiceXML to create equations. (Part 7, 8 of 8.) Outline MathTutorForm.cs ( 7 of 8 ) b) Answering the question incorrectly. c) Answering the question correctly.

76 Outline Figure 30.32 is a modified IEquationGeneratorService interface that returns an Equation in JSON format. IEquation GeneratorService .cs Using the ResponseFormat property to specify a JSON response. Fig | WCF REST service interface to create random equations based on a specified operation and difficulty level.

77 Outline A modified MathTutor application (Fig. 30.17) accesses the EquationGenerator web service. MathTutorForm.cs ( 1 of 8 ) Fig | Math tutor using EquationGeneratorServiceJSON to create equations. (Part 1 of 8.)

78 MathTutorForm.cs ( 2, 3 of 8 ) Requesting an equation from the web service. Creating an object to deserialize Equations from JSON format. Fig | Math tutor using EquationGeneratorServiceJSON to create equations. (Part 2 of 8.) Converting JSON responses into Equation objects.

79 Outline MathTutorForm.cs ( 4 of 8 )
Fig | Math tutor using EquationGeneratorServiceJSON to create equations. (Part 4 of 8.)

80 Outline MathTutorForm.cs ( 5 of 8 )
Fig | Math tutor using EquationGeneratorServiceJSON to create equations. (Part 5 of 8.)

81 Outline MathTutorForm.cs ( 6 of 8 )
Fig | Math tutor using EquationGeneratorServiceJSON to create equations. (Part 6 of 8.)

82 Outline MathTutorForm.cs ( 7 of 8 )
a) Generating a level 2 multiplication equation. Fig | Math tutor using EquationGeneratorServiceJSON to create equations. (Part 7 of 8.)

83 Outline MathTutorForm.cs ( 8 of 8 )
b) Answering the question incorrectly. MathTutorForm.cs ( 8 of 8 ) c) Answering the question correctly. Fig | Math tutor using EquationGeneratorServiceJSON to create equations. (Part 8 of 8.)

84 Fig. 30.18 | Equation class representing a JSON object.
Outline A JSON representation of an Equation object is shown in Fig. 30.18. Equation.cs Fig | Equation class representing a JSON object.


Download ppt " Pearson Education, Inc. All rights reserved."

Similar presentations


Ads by Google