July 3, 2002 - Extending a Class to a Web Service | WebReference

July 3, 2002 - Extending a Class to a Web Service

Yehuda Shiran July 3, 2002
Extending a Class to a Web Service
Tips: July 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Here is what you need to do to convert a class to a Web service:

System.Web.Service namespace
  • Add a directive line, specifying the class's language and namespace
  • Derive the class from the .NET Class Library's
  • WebService class
  • Add the attribute
  • WebMethodAttribute to each method you want to expose as a Web service method
  • Put your code in a file with the extension
  • .asmx and save in your Web server area.

    Let's apply the main steps from above to our PrimeNumbers class. The directive line is the first line of the file and looks like this:

      <%@ WebService Language="JScript" class="PrimeNumbers" %>
    You derive the class from the WebService class by extending it. The WebService class includes a lot of methods that are needed for deploying a Web service. Basing your class on the WebService class provides you with these methods for free. You need focus only on your unique value-adding business logic. Here is how you extend WebService to PrimeNumbers:

      public class PrimeNumbers extends WebService {
    The location of your Web service is on your Web server. If your Web server is IIS and your file is checkIsPrime.asmx for example, you can save it in a directory under c:\inetpub\wwwroot. For example:

      c:\inetpub\wwwroot\Webreference\checkIsPrime.asmx
    From now on, you can refer to this Web service as:

      https://localhost/Webreference/checkIsPrime.asmx
    To learn more about JScript .NET, go to Column 112, JScript .NET, Part VI: Creating IE Web Services.