Consume ws


Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Consume ws

  1. 1. Session 6 Module 2 Calling a Web Service from an ASP.NET Web Page
  2. 2. Tasks for this Module <ul><li>How we’d ideally call a Web service from an ASP.NET Web page </li></ul><ul><li>Creating a Proxy class with wsdl.exe </li></ul><ul><ul><li>Using the Proxy class </li></ul></ul><ul><ul><li>Examining the Proxy class </li></ul></ul><ul><ul><li>Examining wsdl.exe </li></ul></ul><ul><li>Time allotment for this module and questions: 75 minutes </li></ul>
  3. 3. Calling a Web Service from an ASP.NET Web Page <ul><li>Ideally the developer calling a Web service from an ASP.NET Web page should be able to work with the Web service using the same syntax as if she was working with a component local to the Web server. </li></ul><ul><li>Since calling a Web service involves marshalling the ingoing parameters properly and being able to marshal the return parameters properly, obviously performing such actions in an ASP.NET Web page would make the syntax differ wildly from using a local component, where such explicit marshalling was not needed. </li></ul>
  4. 4. Enter the Proxy Class <ul><li>In order to achieve the end goal of treating a Web service call just like a call to a local component, the .NET framework contains a command-line program called wsdl.exe ( W eb S ervice D escription L anguage), which creates a proxy class for a specific Web service. </li></ul><ul><li>This proxy class serves as an intermediate between the ASP.NET Web page and the Web service. ( The diagram on the next slide should help clarify this… ) </li></ul>
  5. 5. The Role of the Proxy Class WEB SERVER #2 Web service WEB SERVER #1 SomePage.aspx an ASP.NET Web page PROXY CLASS STEP 3: The Web service unmarshals the incoming parameters, runs the method, and marshals the output parameters. These are sent back in an HTTP response. STEP 1: The ASP.NET Web page SomePage.aspx instantiates an instance of the Proxy class: Dim objClass = New ProxyClassName And calls one of the Web service methods: objClass . MethodName ( paramList ) STEP 2: The proxy class marshals the parameter list and makes an HTTP request to the Web service sitting on Web Server #2 STEP 4: The proxy class unmarshals the return parameters and passes back the result to the ASP.NET Web page. Transaction complete!
  6. 6. Creating a Proxy Class <ul><li>Creating a Proxy class involves three steps: </li></ul><ul><ul><li>Create the source code for the class, which depends upon the WSDL of the Web service. </li></ul></ul><ul><ul><li>Compile the class into a DLL </li></ul></ul><ul><ul><li>Copy the DLL to the in directory </li></ul></ul><ul><li>Once these three steps are complete we can use our Proxy class to access Web service methods as if they were methods of a local component! </li></ul><ul><li>Visual Studio.NET can perform all of these tasks with the click of a button. We will examine how to do it via the command-line for those who don’t have VS.NET installed. </li></ul>
  7. 7. Creating a Proxy Class (Continued…) <ul><li>When creating a proxy class, you must specify the WSDL for the Web service. This XML-formatted file contains information on the incoming and outgoing parameters. See http://localhost/UCSDTeaching/Session6/SimpleWebService.asmx?WSDL </li></ul><ul><li>Create the proxy class by simply calling wsdl.exe with the full URL of the WSDL as the parameter: </li></ul><ul><li>Wsdl http://localhost/UCSDTeaching/Session6/SimpleWebService.asmx?WSDL </li></ul>
  8. 8. Creating a Proxy Class (Continued…) <ul><li>Once we have the source code we need to compile this code into a DLL. </li></ul><ul><li>Finally, once we have the Proxy class compiled into a DLL, we need to move it to the in directory, since that is the location where our DLLs need to reside to be picked up automatically by ASP.NET. </li></ul><ul><li>Run cl.demo1.txt demo </li></ul>
  9. 9. Using the Proxy Class <ul><li>Once we have the Proxy class compiled and the DLL in the in directory, we can use it through an ASP.NET Web page. </li></ul><ul><li>Show CallSimpleWS.aspx Demo </li></ul><ul><li>Note how, when examining the syntax, it is impossible to tell if the instantiation of the ucsd.Math class is referring to a local or remote component. Mission accomplished! </li></ul>
  10. 10. Examining the Proxy Class <ul><li>Let’s take a moment and examine Math.cs , the source code for the Proxy class created by wsdl.exe </li></ul><ul><li>Examine Math.cs </li></ul><ul><li>Note that the Proxy class is in C# - wsdl.exe provides a command-line switch to specify what language to use. </li></ul><ul><li>Note that a class is created with the same name as the Web service class ( Math , in this example). Also, for each public Web method there is a public method in the Proxy class with the correct input and output parameters. </li></ul>
  11. 11. Examining the Proxy Class ( Continued… ) <ul><li>Note that in this Proxy class the Web service is being called using the SOAP request/response payloads. </li></ul><ul><li>All of the complex behavior in hidden in macros or through methods defined in base classes from which our custom Math class inherits from. </li></ul>
  12. 12. Examining wsdl.exe <ul><li>Wsdl.exe contains only one required parameter, a URL or path to the WSDL for the Web service. The following optional parameters can be specified: </li></ul><ul><ul><li>/language: language – specifies what language the Proxy class should be created in (vb/cs/js). </li></ul></ul><ul><ul><li>/protocol: protocol – specifies the payload protocol to be used (SOAP, HttpGet, HttpPost) </li></ul></ul><ul><ul><li>/namespace: namespace – the namespace of the generated Proxy class. </li></ul></ul><ul><ul><li>/out: filename – the filename of the Proxy class. </li></ul></ul>
  13. 13. Module 2 in Conclusion <ul><li>Ideally, we’d like to be able to call Web services from an ASP.NET Web page in an identical fashion to which we call local components. </li></ul><ul><li>Creating a Proxy class allows us to call a Web service’s methods as if the Web service resided locally. The Proxy class handles the actual HTTP request as well as the marshalling of the input and output parameters. </li></ul><ul><li>The command-line program wsdl.exe can create the source code of the Proxy class for us based upon a Web service’s WSDL. Once this class is compiled and placed in the in directory, it can be used through an ASP.NET Web page. </li></ul>
  14. 14. Questions??? Now would be a great time to ask questions! Don’t you think? So ask away! 