Click here to Skip to main content
15,894,907 members
Articles / Web Development / HTML

A Proxy Generator to WebServices for JavaScript and AJAX

Rate me:
Please Sign up or sign in to vote.
4.71/5 (25 votes)
20 Sep 20058 min read 322.2K   1.6K   82  
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.


Written By
Architect Deutsche Bank AG
Germany Germany
see https://www.mathertel.de

Comments and Discussions