Chapter 13 demonstrated the technologies that support Web Services and how to implement a very basic Web Service, the Commissions Web Service. I will pick up from there and show you how to create a Web Service that returns simple data in a practicable way. Prime numbers ”numbers divisible only by themselves or by the number 1 ”are useful in a tremendous variety of mathematics equations as well as encryption. (Unbreakable encryption relies on huge numbers that are the product of two very large primes. The idea is that by the time any known algorithm can factor the product, the data is no longer relevant.) The Web Service in Listing 14.1 demonstrates how to determine whether a number is prime by using a 2,000-year-old technique devised by Eratosthenes, referred to as the Sieve of Eratosthenes . You can look up Eratosthenes on the Internet for factual details and biographical information, but I will paraphrase the basic mathematics and theory here for brevity. The Sieve of Eratosthenes works on the premise of the product of primes. The basic idea is that all positive nonprime integers other than 0 and 1 are the products of prime numbers. Eratosthenes further resolved that when trying to determine whether a number is prime, it is necessary to check as possible divisors only those numbers less than or equal to the square root of the candidate number. This makes sense simply because if a candidate number has divisors (other than 1 and itself), one of the divisors will be less than or equal to the square root and the other will be greater than or equal to the square root (or the divisor will be the integer square root). Consider the number 7. The square root of 7 is less than 3 because 3 Eratosthenes resolved that if one knew all the prime numbers preceding any possible prime, the unknown number could be evaluated by checking to see whether any known primes less than the square root were divisors. The basic sieve works by beginning with a known prime (2) and calculating all of the numbers greater than 2 in succession, using all the preceding discovered primes as possible divisors for each number in succession. The algorithm is pretty simple. In the next subsection I will demonstrate my implementation of the Sieve of Eratosthenes as a Web method, and following that subsection I will discuss a simple strategy for implementing Web Services. ## Implementing the Sieve of EratosthenesThe basic strategy I employ for all programming is to solve the problem regardless of the way in which I intend a particular consumer to use the solution. Essentially, even though I know I will be presenting a solution as a Web method, I solve the general problem first and then expose the solution for consumers as a Web method. As a good general strategy, always solve a problem in the general sense as methods and a class, if applicable . For example, one method alone does not yield a very good class, but one method might be the first method in a math library. Here is the promised Listing 14.1, demonstrating my recollected interpretation of the Sieve of Eratosthenes. The listing is followed by a brief summarization. ## Listing 14.1 A General Solution for Determining Whether a Positive Integer Is Prime1: Public Class PrimeLibrary 2: Private FPrimes As ArrayList 3: 4: Public Sub New(ByVal Max As Long) 5: FPrimes = New ArrayList() 6: Seed() 7: Initialize(Max) 8: End Sub 9: 10: Public Function IsPrime(ByVal Number As Long) As Boolean 11: Return FPrimes.IndexOf(Number) > -1 12: End Function 13: 14: Public ReadOnly Property Primes() As ArrayList 15: Get 16: Return FPrimes 17: End Get 18: End Property 19: 20: Private Sub Seed() 21: FPrimes.Add(2L) 22: End Sub 23: 24: Private Sub Initialize(ByVal Max As Long) 25: If (Max < 3) Then Return 26: Dim I As Long 27: For I = 3 To Max 28: If (Test(I)) Then FPrimes.Add(I) 29: Next 30: End Sub 31: 32: Private Function Test(ByVal NumberToTest As Long) As Boolean 33: Dim Prime As Long 34: For Each Prime In FPrimes 35: If (NumberToTest Mod Prime = 0) Then Return False 36: If (Prime >= Math.Sqrt(NumberToTest)) Then Return True 37: Next 38: 39: Return True 40: End Function 41: 42: End Class The general solution is comprised of a class with several methods. The constructor initializes an array to store the list of primes. The Now that we have a solution that can be used by any application, I can play the role of class consumer and produce the Web Service. The Web Service can expose as much of the class as I'd like, but in this case we will simply expose the ## Implementing the IsPrime Web Service Visual Studio .NET supports the concept of the solution. Solutions can contain several projects and produce one or more assemblies. Chapter 13 described how project templates in the New Project dialog can create all the basic source code for an ASP.NET Web Service by using the template code. Because the template exists, we only have to worry about implementing our Web methods. Listing 14.2 contains the implementation of
## Listing 14.2 A Web Service That Uses the |

Visual Basic(R) .NET Power Coding

ISBN: 0672324075

EAN: 2147483647

EAN: 2147483647

Year: 2005

Pages: 215

Pages: 215

Authors: Paul Kimmel

flylib.com © 2008-2017.

If you may any questions please contact us: flylib@qtcs.net

If you may any questions please contact us: flylib@qtcs.net