A Simple Web Service
We're going to take as our example a simple class that calculates the value of an index in a Fibonacci series. A Fibonacci series is a series of numbers beginning with 0 and 1, in which the next number in the sequence is calculated by adding the previous two numbers. So, the third number in the sequence is 0 + 1 = 1, the fourth number is 1 + 1 = 2, the seventh is 3 + 5 = 8, and so on. The first seven numbers of the Fibonacci series are 0, 1, 1, 2, 3, 5, and 8. Let's implement this in code.
Fibonacci Application Logic
Below is the logic that allows us to interact with a Fibonacci series. Using VB .NET we would write:
Public Class Fibonacci
Public Function GetSeqNumber (fibIndex As Integer) As Integer
If (fibIndex < 2) Then
Return fibIndex
End If
Dim FibArray(2) As Integer
Dim i As Integer FibArray(0) = 0 FibArray(1) = 1
For i = 2 To fibIndex
FibArray(1) = FibArray(1) + FibArray(0)
FibArray(0) = FibArray(1) - FibArray(0)
Next
Return FibArray(1)
End Function
End Class
Using C# we would write:
public class Fibonacci {
public int GetSeqNumber(int fibIndex){
if (fibIndex < 2)
return fibIndex;
int[] FibArray = {0,1};
for (int i = 1; i< fibIndex; i++){
FibArray[1] = FibArray[0] + FibArray[1];
FibArray[0] = FibArray[1] - FibArray[0];
}
return FibArray[1];
}
}
Both of these sections of code create a class named Fibonacci with a single method, GetSeqNumber, that accepts a single parameter, fibIndex of type Integer/int, and returns the value of the index in the Fibonacci series. The index into the series begins with 0. So, for example, GetSeqNumber(6) returns the value 8.
A two-element array is created and initialized with the first series values of 0 and 1. For these numbers in the series, the value is the same as the index number, so we can just use that as a return value. If the fibIndex parameter is greater than 1, we iterate in a For Next (VB) or for loop (C#), performing the necessary math to calculate the current values. Finally we return the requested element in the array - the last value we calculated in the loop.
This is simply application logic, and is no different from the type of application logic we author when we build ASP.NET pages or components. However, there are some special considerations that we need to be aware of when we design application logic to be a Web Service, such as supported data types and behaviors specific to Web Services. We'll cover those later in the chapter.
Next, let's see what we need to do to enable this simple application logic as a Web Service using ASP.NET. Our goal is that we want another application to be able to easily call and use our Fibonacci class. The difference is that our Fibonacci class needs to be able to communicate using SOAP. Fortunately, ASP.NET helps us to do that very easily.
Fibonacci ASP.NET Web Service
We can use ASP.NET to expose our application logic as a Web Service by simply adding attributes to our code. An attribute is declarative code that adds additional behavior to our code - such as the ability to support SOAP - without us having to add new programmatic code that changes the behavior of our application logic. It sounds complicated, but it's really not.
There are several attributes that a Web Service makes use of. The most notable of which is the WebMethod attribute. The methods or properties that have this attribute are treated as Web Services. They can receive, process, and respond with XML messages. All of the internal serialization and de-serialization (that is, how the data being sent back-and-forth as XML is represented) is handled internally.
Let's look at our Fibonacci code, reworked as an ASP.NET Web Service: Using VB .NET we would write:
<%@ WebService class="Fibonacci"%>
Imports System.Web.Services
Public Class Fibonacci
<WebMethod>
Public Function GetSeqNumber (fibIndex as Integer) as Integer
If (fibIndex < 2) Then
Return fibIndex
End If
Dim FibArray(2) as Integer
Dim i as Integer
FibArray(0) = 0 FibArray(1) = 1
For i = 2 To fibIndex
FibArray(1) = FibArray(1) + FibArray(0)
FibArray(0) = FibArray(1) - FibArray(0)
Next
Return FibArray(1)
End Function
End Class
Using C# we would write:
<%@ WebService Language="C#" class="Fibonacci" %>
using System.Web.Services;
public class Fibonacci : WebService{
[WebMethod] public int GetSeqNumber(int fibIndex){
if (fibIndex < 2)
return fibIndex;
int[] FibArray = {0,1};
for (int i = 1; i< fibIndex; i++){
FibArray[1] = FibArray[0] + FibArray[1];
FibArray[0] = FibArray[1] - FibArray[0];
}
return FibArray[1];
}
}
The application logic for both files remains identical. The difference is that the code now exists within an ASP.NET Web Service source file (Fibonacci_vb.asmx or Fibonacci_cs.asmx), and has some additional declarative code, such as the use of the WebMethod attribute.
We'll discuss the details of these changes in a moment, but first let's test our Web Service.
Testing Our Web Service
Our Fibonacci ASP.NET Web Service code is now a functional Web Service. To test its functionality we can use a browser and request the URL that is represented by the ASP.NET Web Service file.
Note: Keep in mind that a Web Service is not intended to service requests for web browsers. The web browser functionality shown is only for testing purposes, or design-time information. In the next chapter we'll look at an IE 5.5 behavior that allows us to consume Web Services for rich browser clients.
To run our fibonacci_cs.asmx (or fibonacci_vb.asmx file), we must make it accessible through a web server, just as we would do for an ASP.NET page. Once you've saved the code to a file on your web server, you can navigate to it with your browser. You should see something like this:

fibonacci_cs.asmx
ASP.NET provides an ASP.NET Web Service Help file template. On receiving an HTTP Get request, the Web Service Help file template programmatically examines the code in the .asmx file (using reflection), and generates the UI. This includes details such as the class and method name. Later, we'll see how we can get even more information out of this file. The UI also provides some handy ways of testing the functionality of the Web Service.
The template page that does this is called DefaultWsdlHelpGenerator.aspx, and can be found in the \WinNt\Microsoft.NET\Framework\[version] directory. As it's just an ASP.NET page, we can easily customize it for our application. We can, in fact, have custom Help file templates files for different ASP.NET applications.
In the previous screenshot there is a Service Description link. This link provides us with the WSDL (our XML contract for this Web Service). The WSDL for any ASP.NET Web Service is available as [webservice name].asmx?WSDL: so in this case we can access it as fibonacci_cs.asmx?WSDL. We'll return to WSDL in the next chapter when we talk about how we use the WSDL to build a proxy to use a Web Service.
The other link on the page names the GetSeqNumber method. This represents the method that we marked as a WebMethod in our Fibonacci class. If we select this link we are presented with information about the GetSeqNumber method we have exposed as a Web Service: