home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / aspplus / doc / writingservices.aspx < prev   
Encoding:
Text File  |  2000-06-08  |  4.3 KB  |  95 lines

  1. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  2.  
  3. <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
  4.  
  5. <h4>Writing a Simple Web Service</h4>
  6.  
  7. Writing a simple Web Service takes only a few minutes, and can be accomplished in any text editor. The service we will build in this section, MathService, exposes methods for adding, subtracting, dividing, and multiplying two numbers.
  8. At the top of the page is a directive that identifies the file as a Web Service in addition to specifying the language for the service (C#, in this case):
  9.  
  10. <div class="code"><pre>
  11. <%@ WebService Language="C#" %>
  12. </pre></div>
  13.  
  14. <p>
  15. In this same file, we define a class that encapsulates the functionality of our service. This class should be public, and inherit from the <b>WebService</b> base 
  16. class. Each method that will be exposed from the service is flagged with a [WebMethod] custom attribute in front of it. Without this 
  17. attribute, the method will not be exposed from the service.  This is sometimes useful for hiding implementation details called by public 
  18. Web Service methods, or for the case where the WebService class is also used in local applications (a local application can use any 
  19. public class, but only [WebMethod] classes will be remotely accessible via SOAP).
  20. <p>
  21.  
  22. <div class="code"><pre>
  23. using System;
  24. using System.Web.Services;
  25.  
  26. public class MathService : WebService {
  27.  
  28.    [WebMethod]
  29.    public int Add(int a, int b) 
  30.    {
  31.        return a + b;
  32.    }
  33. }
  34. </pre></div>
  35.  
  36.  
  37. <p>
  38. Web Service files are saved under the <b>.asmx</b> file extension.  Like .aspx files, these are automatically compiled by the ASP+ 
  39. runtime when a request to the service is made (subsequent requests are serviced by a cached pre-compiled type object).  
  40. In the case of our simple MathService, we have defined the WebService class in the .asmx file itself.  Note that if an .asmx file is 
  41. simply requested by a browser, the ASP+ runtime returns a description of the SDL contract for that service as a web page.
  42. <p>
  43.  
  44. <table>
  45. <tr>
  46. <td>
  47. <Acme:SourceRef 
  48.   RunSample="/quickstart/aspplus/samples/services/MathService/MathService.asmx" 
  49.   ViewSource="/quickstart/aspplus/samples/services/MathService/MathService.src"
  50.   Icon="/quickstart/aspplus/images/mathservice_asmx.gif"
  51.   Caption="MathService.asmx"
  52.   runat="server" />
  53. </td>
  54. <td>
  55. <Acme:SourceRef 
  56.   RunSample="/quickstart/aspplus/samples/services/MathService/MathService.asmx?SDL" 
  57.   Icon="/quickstart/aspplus/images/service_sdl.gif"
  58.   Caption="MathService.sdl"
  59.   runat="server" />
  60. </td>
  61. </tr>
  62. </table>
  63.  
  64. <h5>Pre-Compiled Web Services</h5>
  65.  
  66. If you have a pre-compiled class that you wish to expose as a Web Service (and this class inherits from WebService and have 
  67. [WebMethod] methods) then you can simply create an .asmx file with only the following line:
  68.  
  69. <div class="code"><pre>
  70. <%@ WebService Class=öMyWebApplication.MyWebServiceö %>
  71. </pre></div>
  72.  
  73. <p>
  74. The MyWebApplication.MyWebService defines the WebService class, and is contained in the /bin subdirectory of the ASP+ application.
  75.  
  76.  
  77. <p>
  78. <h5>Consuming a Web Service from a Client Application</h5>
  79.  
  80. To consume this service is very simple. You simply use the WebServiceUtil application that is included on the SDK to create a proxy class that looks similar to the class defined in the .asmx file. (Though only the [WebMethod] methods will be there!) You then simply compile your code with this proxy class included.
  81. <p>
  82. The WebServiceUtil application takes a variety of command-line options, but the most important is /c:proxy, which tells the tool to create a proxy class and /pa:<uri> option, which takes an URI that is the service, where it can consume an SDL file which describes the service.
  83. <p>
  84. Once the proxy class exists, you can create objects based on it. Each method call made with the object will then go out to the URI of the Web Service (usually as a SOAP request).
  85.  
  86. <p>
  87.  
  88. <Acme:SourceRef 
  89.   RunSample="/quickstart/aspplus/samples/services/MathService/MathServiceClient.aspx" 
  90.   ViewSource="/quickstart/aspplus/samples/services/MathService/MathServiceClient.src"
  91.   Icon="/quickstart/aspplus/images/mathservice_client.gif"
  92.   Caption="MathServiceClient.aspx"
  93.   runat="server" />
  94.  
  95. <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->