Creating a Web Service
To create a web service, open your favorite text editor, such as Notepad or SciTe (http://www.scintilla.org/) for PC or TextEdit for Mac users. ASP.NET supports several languages for web services, but we will be using the C# language because it closely mimics ActionScript.The first line of a web service will declare that we are in fact creating a web service, which language we are using, whether to allow debugging, and any web service classes we will be creating.Here is a generic template for the first line:
In the preceding code, we declare that we are creating a web service in the C# language, that we will allow debugging (very important in case you make minor errors), and that we will be creating a web service class called MyClass.NOTENotice that the first line of the web service falls within the <% %> tags. This is because we want the browser to recognize anything between these two tags as a server-side script. The rest of the web service itself does not require them, but the first line does.After that, you need to provide a few web service namespaces that we will need to produce the correct results:
<%@ WebService Language="c#" debug="true" %>
In this code, we used the keyword using to signify that we will be using the System.IO and the System.Web.Services namespaces.After that, we begin to create the web service methods. These are the methods that will be called from the web service itself; they describe what the service does.First, declare the class of web service:
using System.IO;
using System.Web.Services;
Notice that this class is public, which means it can be called from outside the service itself. After that, we use the class keyword and name our class MyClass. Then we begin to create the service with the System.Web.Services.WebService class.The next step is to begin declaring the web methods. To do this, you use the keyword WebMethod in brackets, along with a description, if desired, that will help anyone looking at the web service tell what each web method is doing.
public class MyClass: System.Web.Services.WebService{
Then create the web method itself declaring whether it is private or public. Before you name the web method, you have to declare what data type will be returned. For example, the following will return an integer data type, so we use the keyword int:
[WebMethod(Description="Description of the Web method")]
Now that you have seen the basic parts of a web service, we can begin to create them.The first web service will simply return a string saying "hello world". So open your favorite text editor and place this code in it:
public int myMethod(){
return 15;
}
}
The preceding code does everything we have discussed so far. It declares that we are creating a web service in C#. It then gets the classes we need to use. After that, it creates the Hello class and then the method sayHello, which will send the string literal "hello world" back to us.Now save as hello.asmx in either your web server or PWS (personal web server). The directory on most web servers including PWSs is at c:\inetpub\wwwroot\or one of its subdirectories. The .asmx extension is the extension for web services on .NET.Map to the new file using the browser, using http:// not file://, and you should see a screen similar to Figure 25.1.
<%@ WebService Language="c#" debug="true" %>
using System.IO;
using System.Web.Services;
public class Hello: System.Web.Services.WebService{
[WebMethod(Description="Say hello")]
public string sayHello() {
return "hello world";
}
}
Figure 25.1. You can test your web methods without an application.
[View full size image]

Figure 25.2. The web method information.
[View full size image]

Figure 25.3. The results from the web service.

Figure 25.4. The WSDL of the web service.
[View full size image]
