Creating a Web Service

To create a web service, open your favorite text editor, such as Notepad or SciTe ( 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:

 <%@ WebService Language="c#" debug="true"  %> 

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.


Notice 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:

 using System.IO; using System.Web.Services; 

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:

 public class MyClass: System.Web.Services.WebService{ 

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.

 [WebMethod(Description="Description of the Web method")] 

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:

 public  int myMethod(){         return 15;     } } 

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:

 <%@ 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";     } } 

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.

Figure 25.1. You can test your web methods without an application.


You must browse to web services to view them in action. Otherwise, the browser will attempt to open them up in an application.

This screen is created automatically to help test the web service without an application. You can see all the web methods listed (in this case, just the sayHello method) and their description if it was declared. Select the sayHello method, and you will be taken to a screen that looks like Figure 25.2. Here you can invoke (run) the method to see its results. Also, you can see all the information about the method, including its return value, and if we had any parameters, they would be shown here as well. Choose the Invoke button, and another browser window will pop up with XML data as shown in Figure 25.3. This is the result of the web service. Now in this case, it's not all that impressive, but as we move forward it will become so.

Figure 25.2. The web method information.

Figure 25.3. The results from the web service.

And the final page to look at is the WSDL, so return to the original page, and at the top, choose the Service Description link; the window will fill with more XML data, as shown in Figure 25.4. This tells any user of the web service everything they need to know to use it. It shows all the methods, their return values, and their parameters.

Figure 25.4. The WSDL of the web service.

Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319 © 2008-2017.
If you may any questions please contact us: