spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column112


JScript .NET, Part VI: Creating IE Web Services

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Creating Web Services

Creating a Web service is very simple, especially when only consumed by the Internet Explorer browser. A Web service is a class method that accomplishes the Web service task. The following example checks if a number is a prime number. It includes the class PrimeNumbers and the method IsPrime():

public class PrimeNumbers {
  public function IsPrime(n:int) : int {
    var i:int;
    if (n % 2 == 0) return (n == 2);
    if (n % 3 == 0) return (n == 3);
    if (n % 5 == 0) return (n == 5);
    for (i=7; i*i <= n; i+=2) {if (n % i == 0) return 0};
    return 1;
  }
}

At the moment, this class does not represent a Web service. Here is what you need to do to convert the class to a Web service:

  • Import the 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.

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 the class above:

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:

http://localhost/Webreference/checkIsPrime.asmx

Here is a complete listing of the Web service:

<%@ WebService Language="JScript" class="PrimeNumbers" %>

import System;
import System.Web;
import System.Web.Services;

public class PrimeNumbers extends WebService {
  WebMethodAttribute public function IsPrime(n:int) : int {
    var i:int;
	if (n % 2 == 0) return (n == 2);
	if (n % 3 == 0) return (n == 3);
	if (n % 5 == 0) return (n == 5);
	for (i=7; i*i <= n; i+=2) {if (n % i == 0) return 0};
	return 1;
  }
}

We are not going into the algorithm of checking for prime numbers, implemented in the above Web service. There are many algorithms around, and a lot of research has already been conducted on the subject. A prime number is a number which is divisible only by itself and by 1.

Also notice that we have imported more namespaces than absolutely required (System.Web.Services). The two other namespaces above (System and System.Web) will be required in future columns.

Next: How to interact with the add Web service

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: June 17, 2002
Revised: June 17, 2002

URL: http://www.webreference.com/js/column112/2.html