- Why Use Web Services?
- Implementing Your First Web Service
- Testing the Web Service
- Implementing the Web Service Client
- Understanding How Web Services Work
- Summary
- Q&A
- Workshop
Implementing Your First Web Service
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:\src\timeservice.
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 1TimeUtils.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 914) 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.