Source Code

for file /AJAXEngine/S02_AJAXCoreSamples/CalcService.asmx

<%@ WebService Language="C#" Class="CalcService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Script.Serialization;


[WebService(Namespace = "http://www.mathertel.de/CalcFactors/",
  Description="A WebService for the calculation of prime factors.")]
// [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
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.")]
  [ScriptMethod(UseHttpGet=true ,ResponseFormat = ResponseFormat.Json)]
  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 from a given number.</summary>
  /// <param name="inputText">A positive number</param>
  /// <returns>the list of all prime 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 prime 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 prime 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


This page is part of the http://www.mathertel.de/ web site.

For updates and discussions see http://ajaxaspects.blogspot.com/.