Chapter 11 Web Services (Code Reuse over the Internet) Yingcai Xiao
What is it? What it is for? Examples How to write one? What’s inside? How does it work?
Web Services A Web service is an application that exposes Web methods over the Web. Proxy of Interface 2 Client 2 Applicat...
<ul><li>Sharing Objects over the Internet: How? </li></ul><ul><ul><li>Transmitting => HTTP  </li></ul></ul><ul><ul><li>Sha...
Web Services (XML Web Services) (Outside)   <ul><li>A Web service is a different kind of Web application.  </li></ul><ul><...
Web Services (Inside) <ul><li>A Web service is an application that: </li></ul><ul><ul><li>Runs on a Web server </li></ul><...
Professional Web Service Examples <ul><li>Microsoft TerraService:  http://terraservice.net/terraservice.asmx </li></ul><ul...
Professional Web Service Examples <ul><li>Web Service started to evolve into Cloud Computing, where the software is in the...
Ways to Write WS <ul><li>There are many ways to write Web services </li></ul><ul><ul><li>write Web services by hand; </li>...
Our First Web Service Using ASP.NET <ul><li>Calc.asmx </li></ul><ul><li><%@ WebService Language=&quot;C#&quot; Class=&quot...
Our First Web Service Using ASP.NET <ul><li>[WebMethod </li></ul><ul><li>(Description=&quot;Computes the difference betwee...
Our First Web Service Using ASP.NET <ul><li>The ASMX file (calc.asmx) is a complete Web service. It implements two Web met...
Our First Web Service Using ASP.NET <ul><li>Important principles of Web service programming using the .NET Framework: </li...
How a Web Service can be tested <ul><li>ASP.NET can generate forms to test web services on the fly from ASMX files. </li><...
<ul><li>Inside Web Services </li></ul>
The WebService Base Class <ul><li>class CalcService : WebService </li></ul><ul><li>WebService belongs to the System.Web.Se...
The WebMethod Attribute <ul><li>The WebMethod attribute tags a method as a Web method and supports the following parameter...
The WebMethod Attribute <ul><li>[WebMethod (EnableSession=&quot;true&quot;, </li></ul><ul><li>Description=&quot;Adds an it...
The Web Services Description Language (WSDL) <ul><li>A new standard for describing web services. </li></ul><ul><li>An XML ...
WSDL Contract <ul><li>A WSDL contract which contains: </li></ul><ul><ul><li>service element that describes the Web service...
Web Services and Complex Data Types <ul><li>Complex types are supported. </li></ul><ul><li>Represented in XML using SOAP. ...
Web Services and Complex Data Types <ul><li>C# Class </li></ul><ul><li>public class Bookstore </li></ul><ul><li>{ </li></u...
Web Services and Complex Data Types <ul><li>You can’t pass complex types to a Web method using HTTP GET and POST. That’s n...
<ul><li>How to Use/Consume Web Services (WS)  </li></ul><ul><ul><ul><li>Internal (low-level) code for using web services. ...
WS Low-level Code <ul><li>Example: a Web service that publishes Web methods named Add and Subtract at www.wintellect.com/c...
WS Low-level Code <ul><li>A client sent a request to add 2 and 2 to the “Add” Web method using SOAP. </li></ul><ul><ul><li...
WS Low-level Code <ul><li>And here’s how the Web service would respond using SOAP: </li></ul><ul><ul><li>HTTP/1.1 200 OK <...
WS Low-level Code <ul><li>The Web service’s job for responding to each request is to  </li></ul><ul><ul><li>receive  input...
<ul><li>.NET Framework also allows Web methods to be invoked using ordinary HTTP GET and POST commands instead of SOAP. Th...
<ul><li>Here’s a POST command that adds 2 and 2: </li></ul><ul><li>POST /calc.asmx/Add HTTP/1.1 </li></ul><ul><li>Host: ww...
Web Services A Web service is an application that exposes Web methods over the Web. Proxy of Interface 2 Client 2 Applicat...
<ul><li>Writing Clients to  </li></ul><ul><li>Consume  </li></ul><ul><li>Web Services </li></ul>
Web Service Clients & Proxies <ul><li>Web service clients—applications that use, or consume, Web methods.  </li></ul><ul><...
Web Service Clients & Proxies <ul><li>Calling the corresponding Web service is a simple matter of calling methods on the p...
Generation of a Web Service Proxy <ul><li>The wsdl.exe utility that comes with the .NET Framework SDK generates Web servic...
Generation of a Web service proxy <ul><li>Wsdl.exe generates a CS file containing a class that represents the Web service ...
A Simple Web Service Client <ul><li>Write a console client (not a web client) for web service Calc.asmx. </li></ul><ul><li...
A Simple Web Service Client <ul><li>3. Compile the CS files into a console application: </li></ul><ul><li>csc CalcConsoleC...
Write an ASP client for Calc.asmx <ul><li>Create a proxy class for Calc.asmx the same way as above.  </li></ul><ul><li>Cre...
Write an ASP client for Calc.asmx <ul><li>3. Compile the CS files into a dll for code behind: </li></ul><ul><li>csc  /targ...
Write an ASP client for Calc.asmx 5. Create a virtual directory: CalcASPClient.  6. Access the “client” through a browser....
The event flow when the user clicks to add: <ul><li>User-> </li></ul><ul><li>Web Client (a browser)-> </li></ul><ul><li>We...
The CityView Application <ul><li>A real Web application as a Web service client.  </li></ul><ul><li>The Web service is the...
The CityView Application <ul><li>Installing the client: </li></ul><ul><li>CityView.aspx: a Web form that defines CityView’...
Web Services and More  <ul><li>Code-Behind  </li></ul><ul><li>Asynchronous </li></ul><ul><li>For-fee </li></ul>
Web Services and Code-Behind <ul><li>Coding:  Calc2.asmx:  <%@ WebService Class=&quot;CalcService&quot; %> </li></ul><ul><...
Asynchronous Method Calls <ul><li>An asynchronous call returns immediately, no matter how long the Web service requires to...
For-Fee Web Services <ul><li>Authenticate Web Service Callers:  </li></ul><ul><li>Assign authorized callers an authenticat...
For-Fee Web Services <ul><li>Server Side: </li></ul><ul><li>public class AuthHeader : SoapHeader </li></ul><ul><li>{  publ...
For-Fee Web Services <ul><li>Client Side: </li></ul><ul><li>SafeService calc = new SafeService (); </li></ul><ul><li>AuthH...
For-Fee Web Services <ul><li>The outgoing SOAP envelope (generated by .NET):  </li></ul><ul><li><?xml version=&quot;1.0&qu...
Web Services A Web service is an application that exposes Web methods over the Web. Proxy of Interface 2 Client 2 Applicat...
<ul><li>Finding Web Services </li></ul><ul><li>On a Server (DISCO) </li></ul><ul><li>On the Internet (UDDI) </li></ul>
Web Service Discovery—DISCO <ul><li>How do clients know that a Web service exists? </li></ul><ul><li>DISCO  (short for “di...
Web Service Discovery—DISCO <ul><li>As an example, to publish two Web services: </li></ul><ul><li>http://www.wintellect.co...
Web Service Discovery—DISCO <ul><li>Or deploy a VSDISCO file to enable dynamic discovery. The following VSDISCO file autom...
Web Service Discovery—UDDI   <ul><ul><li>UDDI   (Universal Description, Discovery, and Integration) is a  global  (Interne...
Web Service Discovery—UDDI   <ul><ul><li>UDDI sites are themselves Web service sites.  </li></ul></ul><ul><ul><li>They pub...
<ul><li>Dawn of a New Era </li></ul><ul><li>True cross-platform distributed computing based on HTTP, XML, and SOAP. </li><...
Upcoming SlideShare
Loading in...5
×

PDN-C11-PPT.ppt

1,558

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,558
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
33
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

PDN-C11-PPT.ppt

  1. 1. Chapter 11 Web Services (Code Reuse over the Internet) Yingcai Xiao
  2. 2. What is it? What it is for? Examples How to write one? What’s inside? How does it work?
  3. 3. Web Services A Web service is an application that exposes Web methods over the Web. Proxy of Interface 2 Client 2 Application 1 UDDI Registry 1 WSDL Interface 1 UDDI Registry 2 Application 2 WSDL Interface 2 SOAP SOAP WEB Proxy of Interface 1 Client 1
  4. 4. <ul><li>Sharing Objects over the Internet: How? </li></ul><ul><ul><li>Transmitting => HTTP </li></ul></ul><ul><ul><li>Sharing => Standard: </li></ul></ul><ul><ul><li>SOAP (Simple Object Access Protocol) for objects </li></ul></ul><ul><ul><li>WSDL (Web Service Definition Language) for services </li></ul></ul><ul><ul><li>Defining => XML for both </li></ul></ul>.
  5. 5. Web Services (XML Web Services) (Outside) <ul><li>A Web service is a different kind of Web application. </li></ul><ul><li>It’s not designed to serve end users. </li></ul><ul><li>It’s designed to provide services to other applications through a highly programmable Internet. </li></ul><ul><li>It doesn’t have a user interface. </li></ul><ul><li>It exposes callable API functions, known as Web methods, over the Internet. </li></ul><ul><li>.NET Framework makes writing Web services and Web service clients easy. </li></ul><ul><li>Web services are not the property of Microsoft. They’re an industry standard built on open protocols such as HTTP and the Simple Object Access Protocol (SOAP). </li></ul><ul><li>You don’t need the .NET Framework to write Web services or Web service clients. </li></ul>
  6. 6. Web Services (Inside) <ul><li>A Web service is an application that: </li></ul><ul><ul><li>Runs on a Web server </li></ul></ul><ul><ul><li>Exposes Web methods to interested callers </li></ul></ul><ul><ul><li>Listens for HTTP requests representing commands to invoke Web methods </li></ul></ul><ul><ul><li>Executes Web methods and returns the results </li></ul></ul><ul><ul><li>Most Web services expect SOAP messages. </li></ul></ul><ul><ul><li>SOAP is an XML-based vocabulary for performing remote procedure calls using HTTP and other protocols. ( http://www.w3.org/TR/SOAP ). </li></ul></ul>
  7. 7. Professional Web Service Examples <ul><li>Microsoft TerraService: http://terraservice.net/terraservice.asmx </li></ul><ul><li>http://winserv1.cs.uakron.edu/xiaotest/CityView.aspx </li></ul><ul><li>Ubid: </li></ul><ul><li>https ://www.ubid.com/app/mypage/Login.aspx </li></ul><ul><li>http://login.live.com/login.srf </li></ul>
  8. 8. Professional Web Service Examples <ul><li>Web Service started to evolve into Cloud Computing, where the software is in the “cloud”. One of the main techniques for Cloud Computing is software as a service (SaaS). </li></ul><ul><li>Amazon E-Commerce Service (ECS) / Amazon Web Services (AWS): </li></ul><ul><li>http://aws.amazon.com/ (Cloud Computing) </li></ul><ul><li>Discussion Forums: http://developer.amazonwebservices.com/connect/forumindex.jspa </li></ul><ul><li>Google Apps: (Cloud Computing) </li></ul><ul><li>http://www.google.com/apps </li></ul><ul><li>http://code.google.com/intl/en/ </li></ul><ul><li>http:// www.google.com/apis/maps / </li></ul><ul><li>http:// maps.google.com / </li></ul><ul><li>300 E Buchtel Ave, Akron, Ohio 44325 </li></ul>
  9. 9. Ways to Write WS <ul><li>There are many ways to write Web services </li></ul><ul><ul><li>write Web services by hand; </li></ul></ul><ul><ul><li>use SOAP toolkits from Microsoft, IBM, and other companies; </li></ul></ul><ul><ul><li>use the .NET Framework (easy, managed). (need to install IIS and ASP.NET, http://www.cs.uakron.edu/~xiao/windows/IISUpdateProcedure.html) </li></ul></ul>
  10. 10. Our First Web Service Using ASP.NET <ul><li>Calc.asmx </li></ul><ul><li><%@ WebService Language=&quot;C#&quot; Class=&quot;CalcService&quot; %> </li></ul><ul><li>using System; </li></ul><ul><li>using System.Web.Services; </li></ul><ul><li>[WebService (Name=&quot;Calculator Web Service&quot;, </li></ul><ul><li>Description=&quot;Performs simple math over the Web&quot;)] </li></ul><ul><li>class CalcService </li></ul><ul><li>{ </li></ul><ul><li>[WebMethod (Description=&quot;Computes the sum of two integers&quot;)] </li></ul><ul><li>public int Add (int a, int b) </li></ul><ul><li>{ </li></ul><ul><li>return a + b; </li></ul><ul><li>} </li></ul>
  11. 11. Our First Web Service Using ASP.NET <ul><li>[WebMethod </li></ul><ul><li>(Description=&quot;Computes the difference between two integers&quot;)] </li></ul><ul><li>public int Subtract (int a, int b) </li></ul><ul><li>{ </li></ul><ul><li>return a - b; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  12. 12. Our First Web Service Using ASP.NET <ul><li>The ASMX file (calc.asmx) is a complete Web service. It implements two Web methods: Add and Subtract. Both take two integers as input and return an integer as well. </li></ul><ul><ul><li>Calc.asmx implements two Web methods: Add and Subtract. </li></ul></ul><ul><ul><li>Deploying: copying it to a directory on your Web server that is URL-addressable (in C:inetpubwwwroot). </li></ul></ul><ul><ul><li>Accessing: </li></ul></ul><ul><ul><li>http://winserv1.cs.uakron.edu/xiaotest/calc.asmx . </li></ul></ul><ul><ul><li>Or map the examples directory as virtual directory Examples, http:// winserv1.cs.uakron.edu /WP/c11/calc/calc.asmx </li></ul></ul><ul><ul><li>(Need to be on the server to test locally.) </li></ul></ul>
  13. 13. Our First Web Service Using ASP.NET <ul><li>Important principles of Web service programming using the .NET Framework: </li></ul><ul><ul><li>ASMX is a file name extension registered to ASP.NET in Machine.config. </li></ul></ul><ul><ul><li>ASMX files begin with @ WebService directives. </li></ul></ul><ul><ul><li>At a minimum, the directive must contain a Class attribute identifying the class that makes up the Web service. </li></ul></ul><ul><ul><li>Web service classes can be attributed with optional WebService attributes. </li></ul></ul><ul><ul><li>Web methods are declared by tagging public methods with WebMethod attributes. </li></ul></ul><ul><ul><li>Helper methods are not exposed by omitting the attribute. </li></ul></ul><ul><ul><li>HTTP, XML, and SOAP are hidden under the hood. </li></ul></ul><ul><ul><li>The Web methods can be invoked with SOAP, HTTP GET, and HTTP POST. </li></ul></ul><ul><ul><li>They can return output in SOAP responses or simple XML wrappers. </li></ul></ul>
  14. 14. How a Web Service can be tested <ul><li>ASP.NET can generate forms to test web services on the fly from ASMX files. </li></ul><ul><li>Therefore, we don’t have to write special clients and GUI to test web services. </li></ul><ul><li>Example: Calc.asmx </li></ul><ul><li>Users call http://winserv1.cs.uakron.edu/xiaotest/Calc.asmx up in a browser. </li></ul><ul><li>IIS sends the request to ASP.NET on winserv1.cs.uakron.edu. </li></ul><ul><li>ASP.NET compiles Calc.asmx into a DLL. </li></ul><ul><li>ASP.NET displays a page that users can test the Add method. </li></ul><ul><li>Users Interact with it by clicking the “Add” button. </li></ul><ul><li>ASP.NET finds the method name and signature by reading them from the metadata in the DLL. </li></ul><ul><li>It generates an HTML form that users can use to call the Add method with choice of inputs. </li></ul><ul><li>When the users type 2 and 2 into the “a” and “b” boxes and click Invoke, the XML returned by the Web method appears in a separate browser window. </li></ul>
  15. 15. <ul><li>Inside Web Services </li></ul>
  16. 16. The WebService Base Class <ul><li>class CalcService : WebService </li></ul><ul><li>WebService belongs to the System.Web.Services namespace. </li></ul><ul><li>It contributes properties named Application, Session, Context, Server, and User to derived classes. </li></ul>
  17. 17. The WebMethod Attribute <ul><li>The WebMethod attribute tags a method as a Web method and supports the following parameters: </li></ul>Specifies the transactional behavior of a Web method TransactionOption Specifies the Web method’s name MessageName Enables and disables session state for this Web method (default: disabled) EnableSession Adds a textual description to a Web method Description Caches responses, in seconds CacheDuration Enables and disables response buffering BufferResponse Description Parameter Name
  18. 18. The WebMethod Attribute <ul><li>[WebMethod (EnableSession=&quot;true&quot;, </li></ul><ul><li>Description=&quot;Adds an item to a shopping cart&quot;)] </li></ul><ul><li>public void AddToCart (Item item) </li></ul><ul><li>{ ShoppingCart cart = (ShoppingCart) Session[&quot;MyShoppingCart&quot;]; </li></ul><ul><li> cart.Add (item); } </li></ul><ul><li>Web methods cannot be overloaded. (Why?) </li></ul><ul><li>[WebMethod (MessageName=&quot;AddInts&quot;)] </li></ul><ul><li>public int Add (int a, int b) </li></ul><ul><li>{ return a + b; } </li></ul><ul><li>[WebMethod (MessageName=&quot;AddFloats&quot;)] </li></ul><ul><li>public float Add (float a, float b) </li></ul><ul><li>{ return a + b; } </li></ul>
  19. 19. The Web Services Description Language (WSDL) <ul><li>A new standard for describing web services. </li></ul><ul><li>An XML vocabulary. </li></ul><ul><li>For machines to read. </li></ul><ul><li>Documented at http://www.w3.org/TR/wsdl . </li></ul><ul><li>Need to publish a WSDL contract when publishing a Web service. </li></ul><ul><li>Other developers can use the contract to write clients for your Web service. Wsdl.exe generates a wrapper class containing all the elements needed to talk to a Web service. </li></ul><ul><li>To generate a WSDL contract : </li></ul><ul><li>http://winserv1.cs.uakron.edu/xiaotest/Calc.asmx?wsdl </li></ul>
  20. 20. WSDL Contract <ul><li>A WSDL contract which contains: </li></ul><ul><ul><li>service element that describes the Web service; </li></ul></ul><ul><ul><li>operation elements that document the “operations,” or Web methods, that the service supports; </li></ul></ul><ul><ul><li>binding elements that document the protocols that the Web methods support; </li></ul></ul><ul><ul><li>other descriptive information. </li></ul></ul>
  21. 21. Web Services and Complex Data Types <ul><li>Complex types are supported. </li></ul><ul><li>Represented in XML using SOAP. </li></ul><ul><li>A client obtains an XML schema describing the data type from the service’s WSDL contract. </li></ul><ul><li>Note: WSDL is for describing web services (special web applications) while SOAP is for describing web objects. Both use XML vocabulary. (books, chapters, paragraphs, English). </li></ul>
  22. 22. Web Services and Complex Data Types <ul><li>C# Class </li></ul><ul><li>public class Bookstore </li></ul><ul><li>{ </li></ul><ul><li>public string Name; </li></ul><ul><li>public string Address; </li></ul><ul><li>public string City; </li></ul><ul><li>public string State; </li></ul><ul><li>… </li></ul><ul><li>} </li></ul><ul><li>SOAP in XML </li></ul><ul><li><s:complexType name=&quot;Bookstore&quot;> </li></ul><ul><li><s:sequence> </li></ul><ul><li><s:element minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot; name=&quot;Name&quot; </li></ul><ul><li>nillable=&quot;true&quot; type=&quot;s:string&quot; /> </li></ul><ul><li><s:element minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot; name=&quot;Address&quot; </li></ul><ul><li>nillable=&quot;true&quot; type=&quot;s:string&quot; /> </li></ul><ul><li><s:element minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot; name=&quot;City&quot; </li></ul><ul><li>nillable=&quot;true&quot; type=&quot;s:string&quot; /> </li></ul><ul><li><s:element minOccurs=&quot;1&quot; maxOccurs=&quot;1&quot; name=&quot;State&quot; </li></ul><ul><li>nillable=&quot;true&quot; type=&quot;s:string&quot; /> </li></ul><ul><li></s:sequence> </li></ul><ul><li></s:complexType> </li></ul>
  23. 23. Web Services and Complex Data Types <ul><li>You can’t pass complex types to a Web method using HTTP GET and POST. That’s not a limitation if you use SOAP to invoke Web methods. (ASP.NET generates test pages using HTTP GET). </li></ul><ul><li>Any fields or properties declared in a class or struct that’s passed to or from a Web method must be public if they’re to be serialized (transmitted or saved) when instances of the class or struct are serialized. That’s because the .NET Framework’s XML serializer will not serialize nonpublic members. </li></ul><ul><li>Examine a WSDL contract: </li></ul><ul><li>http://winserv1.cs.uakron.edu/xiaotest/calc2.asmx?wsdl </li></ul>
  24. 24. <ul><li>How to Use/Consume Web Services (WS) </li></ul><ul><ul><ul><li>Internal (low-level) code for using web services. </li></ul></ul></ul><ul><ul><ul><li>HTTP commands for using web services through a web browser. </li></ul></ul></ul><ul><ul><ul><li>How to write a WS client using proxies. </li></ul></ul></ul><ul><ul><ul><li>A console-based WS client. </li></ul></ul></ul><ul><ul><ul><li>An ASP.NET-based WS client. </li></ul></ul></ul><ul><ul><ul><li>For-fee WSs. </li></ul></ul></ul><ul><ul><ul><li>Searching for WSs. </li></ul></ul></ul>
  25. 25. WS Low-level Code <ul><li>Example: a Web service that publishes Web methods named Add and Subtract at www.wintellect.com/calc.asmx. </li></ul><ul><li>Before sending a request to the web service, the client needs to know the services provided by the web service by reading its WSDL contract. </li></ul><ul><li>http:// tempuri.org / provides namespaces for XML Web Services under development. </li></ul>
  26. 26. WS Low-level Code <ul><li>A client sent a request to add 2 and 2 to the “Add” Web method using SOAP. </li></ul><ul><ul><li>POST /calc.asmx HTTP/1.1 </li></ul></ul><ul><ul><li>Host: www.wintellect.com </li></ul></ul><ul><ul><li>Content-Type: text/xml; charset=utf-8 </li></ul></ul><ul><ul><li>Content-Length: 338 </li></ul></ul><ul><ul><li>SOAPAction: &quot;http://tempuri.org/Add&quot; </li></ul></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul></ul><ul><ul><li><soap:Envelope </li></ul></ul><ul><ul><li>xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; </li></ul></ul><ul><ul><li>xmlns:xsd=http://www.w3.org/2001/XMLSchema </li></ul></ul><ul><ul><li>xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;> </li></ul></ul><ul><ul><li><soap:Body> </li></ul></ul><ul><ul><li><Add xmlns=&quot;http://tempuri.org/&quot;> </li></ul></ul><ul><ul><li><a>2</a> </li></ul></ul><ul><ul><li><b>2</b> </li></ul></ul><ul><ul><li></Add> </li></ul></ul><ul><ul><li></soap:Body> </li></ul></ul><ul><ul><li></soap:Envelope> </li></ul></ul>
  27. 27. WS Low-level Code <ul><li>And here’s how the Web service would respond using SOAP: </li></ul><ul><ul><li>HTTP/1.1 200 OK </li></ul></ul><ul><ul><li>Content-Type: text/xml; charset=utf-8 </li></ul></ul><ul><ul><li>Content Length: 353 </li></ul></ul><ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf8&quot;?> </li></ul></ul><ul><ul><li><soap:Envelope  </li></ul></ul><ul><ul><li>xmlns:xsi=http://www.w3.org/2001/XMLSchemainstance  </li></ul></ul><ul><ul><li>xmlns:xsd=http://www.w3.org/2001/XMLSchema   </li></ul></ul><ul><ul><li>xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;>   </li></ul></ul><ul><ul><li><soap:Body>     </li></ul></ul><ul><ul><li><AddResponse xmlns=&quot;http://tempuri.org/&quot;>       </li></ul></ul><ul><ul><li><AddResult>4</AddResult>     </li></ul></ul><ul><ul><li></AddResponse>   </li></ul></ul><ul><ul><li></soap:Body> </li></ul></ul><ul><ul><li></soap:Envelope> </li></ul></ul>
  28. 28. WS Low-level Code <ul><li>The Web service’s job for responding to each request is to </li></ul><ul><ul><li>receive input from client , </li></ul></ul><ul><ul><li>parse the SOAP envelope containing the inputs, </li></ul></ul><ul><ul><li>compute , </li></ul></ul><ul><ul><li>formulate a SOAP envelope containing the result, </li></ul></ul><ul><ul><li>return it to the client in the body of the HTTP response. </li></ul></ul><ul><li>The WSDL contract is published just once for each web service, not for each request. </li></ul><ul><li>.NET Framework insulates developers from the low-level details of SOAP, HTTP, and XML and provides a high-level framework for writing Web services and Web service clients. </li></ul>
  29. 29. <ul><li>.NET Framework also allows Web methods to be invoked using ordinary HTTP GET and POST commands instead of SOAP. This is a non-OO approach and can’t take care of ComplexType objects. </li></ul><ul><li>Using Get: </li></ul><ul><li>GET /calc.asmx/Add?a=2&b=2 HTTP/1.1 Host: www.wintellect.com </li></ul><ul><li>The Web service responds as follows: </li></ul><ul><li>HTTP/1.1 200 OK </li></ul><ul><li>Content-Type: text/xml; charset=utf-8 </li></ul><ul><li>Content-Length: 80 </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul><ul><li><int xmlns=&quot;http://tempuri.org/&quot;>4</int> </li></ul>WS Using HTTP GET
  30. 30. <ul><li>Here’s a POST command that adds 2 and 2: </li></ul><ul><li>POST /calc.asmx/Add HTTP/1.1 </li></ul><ul><li>Host: www.wintellect.com </li></ul><ul><li>Content-Type: application/x-www-form-urlencoded </li></ul><ul><li>Content-Length: 7 </li></ul><ul><li>a=2&b=2 </li></ul><ul><li>And here’s the Web service’s response: </li></ul><ul><li>HTTP/1.1 200 OK </li></ul><ul><li>Content-Type: text/xml; charset=utf-8 </li></ul><ul><li>Content-Length: 80 </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul><ul><li><int xmlns=&quot;http://tempuri.org/&quot;>4</int> </li></ul>WS Using HTTP POST
  31. 31. Web Services A Web service is an application that exposes Web methods over the Web. Proxy of Interface 2 Client 2 Application 1 UDDI Registry 1 WSDL Interface 1 UDDI Registry 2 Application 2 WSDL Interface 2 SOAP SOAP WEB Proxy of Interface 1 Client 1
  32. 32. <ul><li>Writing Clients to </li></ul><ul><li>Consume </li></ul><ul><li>Web Services </li></ul>
  33. 33. Web Service Clients & Proxies <ul><li>Web service clients—applications that use, or consume, Web methods. </li></ul><ul><li>Web Service Proxies </li></ul><ul><li>A Web service proxy is an object that provides a local representation of a remote Web service. </li></ul><ul><li>A proxy is instantiated in the client’s own application domain, but calls to the proxy flow through the proxy and out to the Web service that the proxy represents. </li></ul>
  34. 34. Web Service Clients & Proxies <ul><li>Calling the corresponding Web service is a simple matter of calling methods on the proxy. </li></ul><ul><ul><li>CalculatorWebService calc = new CalculatorWebService (); </li></ul></ul><ul><ul><li>int sum = calc.Add (2, 2); </li></ul></ul><ul><li>When you call one of these methods, the proxy packages up the input parameters and invokes the Web method using the protocol encapsulated in the proxy (typically SOAP). </li></ul><ul><li>The proxy insulates you from the low-level details of the Web service and of the protocols that it uses. It even parses the XML that comes back and makes the result available as managed types. </li></ul>
  35. 35. Generation of a Web Service Proxy <ul><li>The wsdl.exe utility that comes with the .NET Framework SDK generates Web service proxy classes from WSDL contracts. </li></ul><ul><li>For Web services written with .NET: </li></ul><ul><li> All Programs->…-> Visual Studio Command Prompt </li></ul><ul><li>cd to the directory you are going to write your client </li></ul><ul><li>wsdl http://winserv1.cs.uakron.edu/xiaotest/calc.asmx </li></ul><ul><ul><li>In the lab: </li></ul></ul><ul><ul><li>Z: </li></ul></ul><ul><ul><li>mkdir ws </li></ul></ul><ul><ul><li>cd ws </li></ul></ul><ul><ul><li>wsdl http :// l winserv1.cs.uakron.edu /calc.asmx </li></ul></ul><ul><li>For Web services not written with .NET: </li></ul><ul><li>find the WSDL contract and wsdl calc.wsdl </li></ul>
  36. 36. Generation of a Web service proxy <ul><li>Wsdl.exe generates a CS file containing a class that represents the Web service proxy. </li></ul><ul><li>Use the class to invoke the Web service’s methods. </li></ul><ul><li>The proxy class’s name comes from the service name. </li></ul><ul><li>Example: </li></ul><ul><li>[WebService (Name=&quot;Calculator Web Service&quot;)] </li></ul><ul><li>The resulting <service> tag in the WSDL contract looks like this: </li></ul><ul><li>Name=Calculator_x0020_Web_x0020_Service </li></ul><ul><li>The resulting proxy class is named CalculatorWebService. </li></ul><ul><li>Wsdl.exe switches: </li></ul><ul><li>/out:Calc.cs </li></ul><ul><li>/language:vb </li></ul><ul><li>/namespace:Calc </li></ul><ul><li>/protocol:httpget </li></ul><ul><li>/protocol:httppost </li></ul><ul><li>/proxy:http://myproxy </li></ul>
  37. 37. A Simple Web Service Client <ul><li>Write a console client (not a web client) for web service Calc.asmx. </li></ul><ul><li>Use Wsdl.exe to create a proxy class for Calc.asmx. </li></ul><ul><li>wsdl http://winserv1.cs.uakron.edu / xiaotest/calc.asmx </li></ul><ul><li>or wsdl http://winserv1.cs.uakron.edu/WP/c11/calc/calc.asmx </li></ul><ul><li>wsdl.exe responds by creating a file named </li></ul><ul><li>CalculatorWebService.cs </li></ul><ul><li>which contains the proxy class. </li></ul><ul><li>Create a new text file named CalcConsoleClient.cs to use the proxy class. </li></ul><ul><ul><ul><li>using System; </li></ul></ul></ul><ul><ul><ul><li>class MyApp </li></ul></ul></ul><ul><ul><ul><li>{ public static void Main () </li></ul></ul></ul><ul><ul><ul><li>{ CalculatorWebService calc = new CalculatorWebService (); </li></ul></ul></ul><ul><ul><ul><li> int sum = calc.Add (2, 2); </li></ul></ul></ul><ul><ul><ul><li>Console.WriteLine (&quot;2 + 2 = &quot; + sum); </li></ul></ul></ul><ul><ul><ul><li>} } </li></ul></ul></ul>
  38. 38. A Simple Web Service Client <ul><li>3. Compile the CS files into a console application: </li></ul><ul><li>csc CalcConsoleClient.cs CalculatorWebService.cs </li></ul><ul><li>Run CalcConsoleClient.exe. </li></ul><ul><li>5. The WS client program instantiates a Web service proxy, calls the service’s Add method, and displays the result. </li></ul>
  39. 39. Write an ASP client for Calc.asmx <ul><li>Create a proxy class for Calc.asmx the same way as above. </li></ul><ul><li>Create a new CS file named CalcASPClient.cs to use the proxy class. </li></ul><ul><ul><li>using System; using System.Web.UI; </li></ul></ul><ul><ul><li>using System.Web.UI.WebControls; </li></ul></ul><ul><ul><li>public class CalcASPClient : Page </li></ul></ul><ul><ul><li>{ protected Label Sum; </li></ul></ul><ul><ul><li>protected TextBox op1, op2; </li></ul></ul><ul><ul><li>public void OnAdd (Object sender, EventArgs e) </li></ul></ul><ul><ul><li>{ int a = Convert.ToInt32 (op1.Text); </li></ul></ul><ul><ul><li>int b = Convert.ToInt32 (op2.Text); </li></ul></ul><ul><ul><li>CalculatorWebService calc = new CalculatorWebService (); </li></ul></ul><ul><ul><li>int c = calc.Add (a, b); Sum.Text = c.ToString (); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  40. 40. Write an ASP client for Calc.asmx <ul><li>3. Compile the CS files into a dll for code behind: </li></ul><ul><li>csc /target:library /out:binCalcASPClient.dll CalcASPClient.cs CalculatorWebService.cs </li></ul><ul><li>4. Create a new ASP file named CalcASPClient.aspx file to use the code behind. </li></ul><%@ Page Inherits=&quot;CalcASPClient&quot; %> <html> <body> <form runat=&quot;server&quot;> <asp:TextBox ID=&quot;op1&quot; RunAt=&quot;server&quot; /> + <asp:TextBox ID=&quot;op2&quot; RunAt=&quot;server&quot; /> <asp:Button Text=&quot;=&quot; OnClick=&quot;OnAdd&quot; RunAt=&quot;server&quot; /> <asp:Label ID=&quot;Sum&quot; RunAt=&quot;server&quot; /> </form> </body> </html>
  41. 41. Write an ASP client for Calc.asmx 5. Create a virtual directory: CalcASPClient. 6. Access the “client” through a browser. http://winserv1.cs.uakron.edu / xiaotest/ CalcASPClient.aspx
  42. 42. The event flow when the user clicks to add: <ul><li>User-> </li></ul><ul><li>Web Client (a browser)-> </li></ul><ul><li>Web Server (IIS) where Web app CalcASPClient.aspx is -> </li></ul><ul><li>ASP.NET-> </li></ul><ul><li>ASP Application (CalcASPClient.aspx and CalcASPClient::OnAdd)-> </li></ul><ul><li>Web Service Proxy (CalculatorWebService::Add) -> </li></ul><ul><li>Web Server (IIS) where Calc.asmx is-> </li></ul><ul><li>ASP.NET-> </li></ul><ul><li>Web Service (CalcService::Add)-> </li></ul><ul><li>Web Service Client (Web Server (IIS) where CalcASPClient.aspx is) -> </li></ul><ul><li>Web Client of Web Application CalcASPClient.aspx -> </li></ul><ul><li>User. </li></ul>
  43. 43. The CityView Application <ul><li>A real Web application as a Web service client. </li></ul><ul><li>The Web service is the Microsoft TerraService ( http:// terraservice.net/terraservice.asmx ). </li></ul><ul><li>It is a Web service front end to the Microsoft TerraServer database. </li></ul><ul><li>TerraServer ( http:// terraservice.net ) is one of the world’s largest online databases of photographs and maps of the Earth’s surface. </li></ul><ul><li>TerraService exposes TerraServer’s content via Web methods. Its WSDL contract is available at http:// terraservice.net/terraservice.asmx?wsdl . </li></ul>
  44. 44. The CityView Application <ul><li>Installing the client: </li></ul><ul><li>CityView.aspx: a Web form that defines CityView’s user interface. </li></ul><ul><li>CityView.ashx: an HTTP handler that returns the requested image. </li></ul><ul><li>TerraService.dll contains a TerraService proxy class named TerraService. </li></ul><ul><li>http://winserv1.cs.uakron.edu / xiaotest/ CityView.aspx </li></ul>
  45. 45. Web Services and More <ul><li>Code-Behind </li></ul><ul><li>Asynchronous </li></ul><ul><li>For-fee </li></ul>
  46. 46. Web Services and Code-Behind <ul><li>Coding: Calc2.asmx: <%@ WebService Class=&quot;CalcService&quot; %> </li></ul><ul><li>Calc.cs: contains the class codbe and needs to be compiled into binCalc.dll. </li></ul><ul><li>Deploying: Root must be a virtual directory (WS-Calc). http://localhost/calc2.asmx </li></ul><ul><li>Benefits: (a) Catches compilation errors before the service is deployed. (b) Enables to write Web services in languages that ASP.NET doesn’t natively support. </li></ul>
  47. 47. Asynchronous Method Calls <ul><li>An asynchronous call returns immediately, no matter how long the Web service requires to process the call. </li></ul><ul><li>To retrieve the results from an asynchronous call, you make a separate call later on by setting up a callback function. </li></ul><ul><li>AsyncCallback cb = new AsyncCallback (AddCompleted); </li></ul><ul><li>IAsyncResult res = calc.BeginAdd (2, 2, cb, null); </li></ul><ul><li>public void AddCompleted (IAsyncResult res) </li></ul><ul><li>{ int sum = calc.EndAdd (res); } </li></ul>
  48. 48. For-Fee Web Services <ul><li>Authenticate Web Service Callers: </li></ul><ul><li>Assign authorized callers an authentication key and require them to pass the key in each method call. </li></ul><ul><li>Transmit user credentials in HTTP Authorization headers. </li></ul><ul><li>Transmit user credentials in SOAP headers. </li></ul><ul><li>System.Web.Services.Protocols.SoapHeader </li></ul>
  49. 49. For-Fee Web Services <ul><li>Server Side: </li></ul><ul><li>public class AuthHeader : SoapHeader </li></ul><ul><li>{ public string UserName; </li></ul><ul><li>public string Password; </li></ul><ul><li>} </li></ul><ul><li>class SafeService </li></ul><ul><li>{ public AuthHeader header; </li></ul><ul><li>[WebMethod] </li></ul><ul><li>[SoapHeader (&quot;header&quot;, Required=&quot;true&quot;)] </li></ul><ul><li>public int Add (int a, int b) </li></ul><ul><li>{ if (header.UserName == &quot;jeffpro&quot; && </li></ul><ul><li>header.Password == &quot;imbatman&quot;) </li></ul><ul><li>return a + b; </li></ul><ul><li>else throw new HttpException (401, &quot;Not authorized&quot;); </li></ul><ul><li>}} </li></ul>
  50. 50. For-Fee Web Services <ul><li>Client Side: </li></ul><ul><li>SafeService calc = new SafeService (); </li></ul><ul><li>AuthHeader header = new AuthHeader (); </li></ul><ul><li>header.UserName = &quot;jeffpro&quot;; </li></ul><ul><li>header.Password = &quot;imbatman&quot;; </li></ul><ul><li>calc.AuthHeaderValue = header; </li></ul><ul><li>int sum = calc.Add (2, 2); </li></ul>
  51. 51. For-Fee Web Services <ul><li>The outgoing SOAP envelope (generated by .NET): </li></ul><ul><li><?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> </li></ul><ul><li><soap:Envelope xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; </li></ul><ul><li>xmlns:xsd=http://www.w3.org/2001/XMLSchema </li></ul><ul><li>xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;> </li></ul><ul><li><soap:Header> </li></ul><ul><li><AuthHeader xmlns=&quot;http://tempuri.org/&quot;> </li></ul><ul><li><UserName>jeffpro</UserName> </li></ul><ul><li><Password>imbatman</Password> </li></ul><ul><li></AuthHeader> </li></ul><ul><li></soap:Header> </li></ul><ul><li><soap:Body> </li></ul><ul><li><Add xmlns=&quot;http://tempuri.org/&quot;> </li></ul><ul><li><a>2</a> </li></ul><ul><li><b>2</b> </li></ul><ul><li></Add> </li></ul><ul><li></soap:Body> </li></ul><ul><li></soap:Envelope> </li></ul>
  52. 52. Web Services A Web service is an application that exposes Web methods over the Web. Proxy of Interface 2 Client 2 Application 1 UDDI Registry 1 WSDL Interface 1 UDDI Registry 2 Application 2 WSDL Interface 2 SOAP SOAP WEB Proxy of Interface 1 Client 1
  53. 53. <ul><li>Finding Web Services </li></ul><ul><li>On a Server (DISCO) </li></ul><ul><li>On the Internet (UDDI) </li></ul>
  54. 54. Web Service Discovery—DISCO <ul><li>How do clients know that a Web service exists? </li></ul><ul><li>DISCO (short for “discovery”) is a file-based protocol for local Web service discovery—that is, for getting a list of available Web services from DISCO files deployed on Web servers. </li></ul><ul><li>Publish a DISCO file on your Web server that describes the Web services. </li></ul><ul><li>Clients interrogate the DISCO file to find out what Web services are available and where the services’ WSDL contracts can be found. </li></ul><ul><li>DISCO’s chief disadvantage is that you can’t read a DISCO file if you don’t have its URL. </li></ul>
  55. 55. Web Service Discovery—DISCO <ul><li>As an example, to publish two Web services: </li></ul><ul><li>http://www.wintellect.com/calc.asmx </li></ul><ul><li>http://www.wintellect.com/locator.asmx </li></ul><ul><li>Deploy the following DISCO file at a well-known URL on your server. The contractRef elements identify the URLs of the Web services’ WSDL contracts. </li></ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul><ul><li><discovery xmlns=&quot;http://schemas.xmlsoap.org/disco/&quot; </li></ul><ul><li>xmlns:scl=&quot;http://schemas.xmlsoap.org/disco/scl/&quot;> </li></ul><ul><li><scl:contractRef ref=&quot;http://www.wintellect.com/calc.asmx?wsdl&quot; </li></ul><ul><li>docRef=&quot;http://www.wintellect.com/calc.asmx&quot; /> </li></ul><ul><li><scl:contractRef ref=&quot;http://www.wintellect.com/locator.asmx?wsdl&quot; </li></ul><ul><li>docRef=&quot;http://www.wintellect.com/locator.asmx&quot; /> </li></ul><ul><li></discovery> </li></ul>
  56. 56. Web Service Discovery—DISCO <ul><li>Or deploy a VSDISCO file to enable dynamic discovery. The following VSDISCO file automatically exposes all ASMX and DISCO files in a host directory and its subdirectories, with the exception of those subdirectories noted with exclude elements: </li></ul><ul><li><?xml version=&quot;1.0&quot; ?> </li></ul><ul><li><dynamicDiscovery </li></ul><ul><li>xmlns=&quot;urn:schemas-dynamicdiscovery:disco.2000-03-17&quot;> </li></ul><ul><li><exclude path=&quot;_vti_cnf&quot; /> </li></ul><ul><li><exclude path=&quot;_vti_pvt&quot; /> </li></ul><ul><li></dynamicDiscovery> </li></ul>
  57. 57. Web Service Discovery—UDDI <ul><ul><li>UDDI (Universal Description, Discovery, and Integration) is a global (Internet) Web service directory that is itself implemented as a Web service. </li></ul></ul><ul><ul><li>Developed by IBM, Microsoft, and Ariba. </li></ul></ul><ul><ul><li>A specification for building distributed databases that enable interested parties to “discover” each other’s Web services. </li></ul></ul><ul><ul><li>No one company owns the databases; anyone is free to publish a UDDI-based business registry. </li></ul></ul><ul><ul><li>Operator sites have already been established by IBM and Microsoft. </li></ul></ul>
  58. 58. Web Service Discovery—UDDI <ul><ul><li>UDDI sites are themselves Web service sites. </li></ul></ul><ul><ul><li>They publish a pair of SOAP-based APIs: </li></ul></ul><ul><ul><ul><li>an inquiry API for inquiring about companies and their Web services </li></ul></ul></ul><ul><ul><ul><li>a publisher API for advertising a company’s Web services (operator sites typically limit the publisher API to registered members). </li></ul></ul></ul><ul><ul><li>Most developers will use high-level tools to query UDDI business registries and generate wrapper classes that allow them to place calls to the Web services. </li></ul></ul>
  59. 59. <ul><li>Dawn of a New Era </li></ul><ul><li>True cross-platform distributed computing based on HTTP, XML, and SOAP. </li></ul><ul><li>Summary: </li></ul><ul><li>Web Services (Architecture), Web Methods, WSDL, SOAP, XML, DISCO, UDDI, Web Service Clients (Stand Alone, Web Based), Web Service Proxies, For-Fee Web Services. </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×