Creating a Web Service
Assuming that you've installed both Internet Information Services and Visual Studio .NET on your development computer, this section will show you how to create a Web Service. This description is just an "appetizer," to give you a feel for what .NET can do and how to use it. This Web Service will be quite limited in terms of its functionality but will offer a taste of how easy development can be using .NET. Web Services will be covered in detail in future lessons. As is traditional for first programs, this Web Service will deliver a "Hello World" message. This section uses Visual Studio. If you want to create a Web Service "by hand" using the Visual Basic compiler and without using Visual Studio, glance at Day 13's lesson.
To create a Web Service, follow these steps:
Start Visual Studio. Click the Get Started option on the left to view the Start Page (see Figure 1.3).
Click the New Project button to open the New Project dialog. This dialog contains two main panes. On the left, you can select from different project types. On the right, you can select project templates for each kind of project that you might create.
Select Visual Basic Projects from the Project Types pane. Select ASP.NET Web Service from the Templates pane. Name the service FirstService (see Figure 1.4) and click OK to create a new virtual directory and several stock files commonly used in a Web Service project.
Look at the new project. The Solution Explorer window is located at the upper right. You can use the Solution Explorer to browse for any files in your project.
Uncomment the HelloWorld method and remove the rest of the template code so that your Service1.asmx file looks like Listing 1.2.
Press F5 to test the Web Service. Pressing this key will compile the project and launch a new instance of Internet Explorer. You should see something similar to Figure 1.5.
Figure 1.3. The Visual Studio Start Page.
Figure 1.4. Naming a new Web Service project.
A virtual directory is a directory name that corresponds to a directory on your hard disk. For instance, a virtual directory named Documents might be linked to the directory C:\DocumentArchive\Current. When a Web browser requests a page in the virtual directory, such as http://localhost/Documents/SomePage.htm, the Web server looks for the file C:\DocumentArchive\Current\SomePage.htm.
Internet Information Services uses the directory C:\Inetpub\wwwroot as the base path for new virtual directories. In step 3, Visual Studio created a new directory called C:\Inetpub\wwwroot\FirstService and a virtual directory called FirstService.
Right-click the Service1.asmx file in the Solution Explorer and select View Code.
Listing 1.2 Service1.asmx with HelloWorld Uncommented
Imports System.Web.Services Public Class Service1 Inherits System.Web.Services.WebService <WebMethod()> Public Function HelloWorld() As String HelloWorld = "Hello World" End Function End Class
Figure 1.5. A first Web Service.
You've now created a working Web Service, albeit a simple one. To use the Web Service, click the HelloWorld link and then click the Invoke button to call the HelloWorld method. By clicking Invoke, you are using Internet Explorer as a Web Service client. We'll defer a full explanation for how Web Services like this work until Day 13.