Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » XML » Web Services » Downloads
 
Add your own
alternative version

A proxy generator to WebServices for JavaScript and AJAX

, 20 Sep 2005
Calling a server from JavaScript is a fundamental part of AJAX applications. Using WebServices with SOAP and WSDL is easy if proxy objects and methods are available in the browser.
<%@ WebService Language="C#" Class="CalcService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace = "http://www.mathertel.de/CalcFactors/",
  Description="A WebService for the calculation of prime factors.")]
// [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class CalcService : System.Web.Services.WebService {
  
  /// <summary>
  /// HelloWorld can live side by side with other methods in the same service.
  /// </summary>
  /// <returns>Hello World</returns>
  [WebMethod(Description = "Say hello to the world.")]
  public string HelloWorld(string who) {
    return "Hello " + who;
  }


  /// <summary>
  /// SlowWorld helps to analyse too long running calls and the client-side timeout feature.
  /// </summary>
  /// <param name="duration">Number of seconds to wait before returning.</param>
  /// <returns>Hello World</returns>
  [WebMethod(Description = "Wast a lot of time, but no cpu.")]
  public string SlowWorld(int duration) {
    System.Threading.Thread.Sleep(duration * 1000);
    return (String.Format("Wasted {0} seconds", duration));
  }

  
  [WebMethod(Description = "Add 2 numbers.")]
  public Int64 AddInteger(Int64 number1, Int64 number2) {
    return (number1+number2);
  }


  [WebMethod(Description = "Add 2 doubles.")]
  public Double AddDouble(Double number1, Double number2) {
    return (number1 + number2);
  }

  
  /// <summary>
  /// Calculate all prime factors of a given number.
  /// </summary>
  /// <param name="inputText">a positive number</param>
  /// <returns>the list of all prie factors</returns>
  [WebMethod(Description="Calculate all prime factors of a given number.")]
  public string CalcPrimeFactors(string inputText) {
    string outputText = String.Empty;
    UInt64 prime;  // try this factor (only primes will match!)
    UInt64 number; // product of the remaining factors

    if ((inputText == null) || (inputText.Length == 0) || (inputText == "0"))
      return (null);

    prime = 2; // start with 2
    number = UInt64.Parse(inputText);

    while ((number > 1) && (prime * prime <= number)) {
      if (number % prime != 0) {
        // try the next factor (slowly)
        prime += (prime == 2UL ? 1UL : 2UL); 

      } else {
        // found a factor !
        outputText = outputText + " " + prime;
        number = number / prime;
      } // if
    } // while

    if (number > 1) {
      // the last factor (a prime) is here.
      outputText = outputText + " " + number;
    }

    return (outputText.Trim());

  } // CalcPrimeFactors


  /// <summary>
  /// Calculate all prime factors of a given number.
  /// </summary>
  /// <param name="number">a positive number</param>
  /// <returns>the list of all prie factors</returns>
  [WebMethod(Description = "Calculate all prime factors of a given number.")]
  public string CalcPrimeFactors2(UInt64 number) {
    string outputText = String.Empty;
    UInt64 prime;  // try this factor (only primes will match!)

    prime = 2; // start with 2

    while ((number > 1) && (prime * prime <= number)) {
      if (number % prime != 0) {
        // try the next factor (slowly)
        prime += (prime == 2UL ? 1UL : 2UL);

      } else {
        // found a factor !
        outputText = outputText + " " + prime;
        number = number / prime;
      } // if
    } // while

    if (number > 1) {
      // the last factor (a prime) is here.
      outputText = outputText + " " + number;
    }
    return (outputText.Trim());
  } // CalcPrimeFactors2
  
  
  /// <summary>
  /// Calculate all prime factors of a given number with a slow response.
  /// </summary>
  /// <remarks>Use this method if your CPU is too fast.</remarks>
  /// <param name="inputText">a positive number</param>
  /// <returns>the list of all prie factors</returns>
  [WebMethod(Description = "Calculate all prime factors of a given number with a slow response. <br /><b>Use this method if your CPU is too fast.</b>")]
  public string SlowCalcPrimeFactors(string inputText) {
    string outputText = String.Empty;
    UInt64 prime;  // try this factor (only primes will match!)
    UInt64 number; // product of the remaining factors

    if ((inputText == null) || (inputText.Length == 0) || (inputText == "0"))
      return (null);

    prime = 2; // start with 2
    number = UInt64.Parse(inputText);

    while ((number > 1) && (prime * prime <= number)) {
      if (number % prime != 0) {
        // try the next factor (slowly)
        prime += 1;

      } else {
        // found a factor !
        outputText = outputText + " " + prime;
        number = number / prime;
        System.Threading.Thread.Sleep(500);
      } // if
    } // while

    if (number > 1) {
      // the last factor (a prime) is here.
      outputText = outputText + " " + number;
    }

    return (outputText);

  } // SlowCalcPrimeFactors

} // class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Matthias Hertel
Architect Deutsche Bank AG
Germany Germany
see http://www.mathertel.de
Follow on   Google+

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150129.1 | Last Updated 20 Sep 2005
Article Copyright 2005 by Matthias Hertel
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid