Implementing your first Web service

Hands on establishment of a simple Web service.

Implementing your first Web service
By Peter Aitken and Phil Syme

This tip, excerpted from InformIT, discusses the development of a general, if simple, Web service. It can be used to provide a basis for developing more specific, and useful, Web services.

Peter Aitken and Phil Syme are the authors of Sams Teach Yourself C# Web Programming in 21 Days.


Rather than dwell on the theoretical aspects of Web services, let's implement a simple Web service to demonstrate some of the points from the preceding section. Two pieces are required in any Web service implementation: the service and the client. We will create the service first.

The easiest way to create Web services in .NET is to build them using ASP.NET. Web service files in ASP.NET have the special filename extension .asmx. ASP.NET flags any file with this extension as a Web service and compiles it as a Web service.

The first sample service that we will create simply returns the current time on the server as a string. Follow these steps to create the service:

Create a directory for the Web service project on your computer. For this example, use the directory C:srctimeservice.

On your Web server, create a virtual directory named TimeService that points to the project directory in step 1.

In the project directory, create a new file called TimeUtilities.asmx using your text editor or download this file from the book's Web site at www.samspublishing.com. Type the contents of Listing 1 if you didn't download the file.

Listing 1 shows a basic Web Service that will return the time of day as a string on the server.

Listing 1 -- TimeUtils.asmx: Returning the Current Time on the Server

 1: <%@ WebService Language="C#" Class="TimeUtilities" %>
 2: using System;
 3: using System.Web.Services;
 4:
 5: [WebService(Namespace="http://tempuri.org/webservices")]
 6: public class TimeUtilities : WebService
 7: {
 8:  [WebMethod]
 9:  public String GetTime()
10:  {
11:   String ret = "The current time on the server is: ";
12:   String curTime = DateTime.Now.ToString();
13:   return ret + curTime;
14:  }
15: }

Line 1 uses the WebService page directive. You've probably seen other ASP.NET page directives such as Page, Import, and Control. The WebService page directive marks the remaining code in the page as a Web service. Within the page directive, you must specify the class of the Web service that you are going to define in the code. Each Web service must have its own class; the one in this example is called TimeUtilities. Lines 2 and 3 specify that we are using the System and System.Web.Services namespaces. They are required in any Web service that we write.

On Line 5 comes the definition of the class. Notice that before we define the class, we use an attribute for it: [WebService(Namespace="http://tempuri.org/webservices")] This line is called an attribute definition. If you are an experienced .NET code developer, you may already have used these kinds of attributes in your code. If you haven't used them, just understand that they supplement the code you write. In this case, we are using the WebService attribute to specify the namespace for our Web service.

Because Web services can be exposed to the rest of the world on the Internet, each one needs a unique namespace. This namespace is exactly the same kind of thing you would use in an XML file that you were going to share with another company. If you are developing a Web service for an organization, you should use your organization's public URL instead of tempuri.org.

Next, Line 6 defines the TimeUtilities class. Because the class defines a Web service, it must derive from the WebService class in the System.Web.WebServices namespace.

Our next order of business (Lines 9-14) is to define the method that we want our Web service users to call. Notice that the GetTime method is preceded by the [WebMethod] attribute. All methods that should be available to Web service users must have this attribute. GetTime simply returns a string with the current time on the server.


To read the entire article from which this tip is excerpted, click over to InformIT. You'll have to register there, but the registration is free.


This was first published in February 2002
This Content Component encountered an error

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close