Published on ONDotNet.com (http://www.ondotnet.com/)
 See this if you're having trouble printing code examples

Build a Simple Web Service

by Hamid Shojaee

In this article we're going to develop a very simple web service using ASP.NET available in Visual Studio.NET Beta 1 which exposes the following functions:

We will then use the automatically generated web service page to use these "web methods" to perform an addition.

If you don't know what a web service is and want to get a quick overview, you may want to read Introduction To Web Services", then come back to this article.


To be able to use this article to develop your first web service, you need to have the following products already installed:

Make sure these products are installed and working in good order before continuing with this article. Also, the examples in this article use Visual Basic.NET (or Visual Basic 7.0) syntax. Familiarity with Visual Basic is a requirement to understand this article.

Creating the Project in Visual Studio.NET

To get started, first create a new web service project in Visual Studio .NET:

  1. From the File menu, select "New" and then "Project"
  2. In the "New Project" window (see below), select "Visual Basic Projects" under the "Project Types" list box and "Web Service" from the "Templates" list box.
  3. For the Name, type "MathLibrary" and for Location, keep the default, which should be "http://[your computer name]"

Screen shot.

Hit OK to create the project. At this time Visual Studio creates a number of files for your web service project including files on the IIS server that will allow your service to operate over the Internet. Even though there is no functionality in the web service project yet, Visual Studio has created a full blown, operational web service.

Writing Your First Web Service

The main file that we will work with in this project is the Service1.asmx file. This file (actually the code behind this file) contains the functions that your web service will expose. Open the code behind the Service1.asmx page by right mouse clicking on the file name (from the Solution Explorer window) and choosing "View Code." You will notice that Visual Studio has already added nearly 50 lines of code. Since we want to develop a simple math library, we're going to remove all the code that was generated in the Service1.asmx "code behind" file and replace it with just a few lines of code that provide us with the math functionality we intend to create:

Imports System.Web.Services
Public Class Service1
  Inherits System.Web.Services.WebService
  Public Function <WebMethod()> Add(ByVal n1 As 
      Integer, _ ByVal n2 As Integer) As Integer
    Return n1 + n2
  End Function
  Public Function <WebMethod()> Multiply(ByVal n1 As
       Integer, _ ByVal n2 As Integer) As Integer
    Return n1 * n2
  End Function
End Class

Let's go through this code, in small chunks that we can easily understand. The first line,

Imports System.Web.Services

allows us to use a certain set of functionality provided by the .NET Framework that pertains to web services. This is a set of libraries that Microsoft has already developed for developers to use.

Next, we see:

Public Class Service1
    Inherits System.Web.Services.WebService

These two lines of code create a new class, called "Service1", which inherits some the web service functionality from the "WebService" class that Microsoft has developed and made available through the System.Web.Services library. We imported that library in the first line of code.

Next, we have our first web method (or function in this case):

Public Function <WebMethod()> Add(ByVal n1 As Integer, _
    ByVal n2 As Integer) As Integer
  Return n1 + n2
End Function

P2P and Web Services Speaker

Learn more about web services at the O'Reilly Peer-to-Peer and Web Services conference. Several web services sessions are planned, with speakers like Clay Shirkey, R.V. Guha, Simon Phipps, David Stutz and many more.

Notice that this function looks exactly like a normal Visual Basic function, with the one exception that it has a <WebMethod()> prefix before the function name. The WebMethod() prefix tells the compiler to make this function available as a function that can be called through the web. The contents of the function could be anything you develop. You can access a database and send the number of records. You can write a function that even returns the records of a table in a database. There are no limits. Practically anything that can be done in a normal function can be done in a web function that's invoked over the web.

That's all there is to it. We have now created our web service with two functions exposed as web methods. Lets use them!

Using the Web Service

Before we can use our Service1 web service, we need to do a full compile. This is done by using the "Build" command from the "Build" menu in Visual Studio .NET. Once the project has been built, Visual Studio .NET has created a formal XML definition of our Service1 WebService (a SDL contract) and a page that allows us to invoke our Service1 web methods. To go to that page, you can either "Start" your web service from the Debug menu of Visual Studio .NET or you can go to the Service1.asmx page on your local PC. When you start the project, you will see a page like this:

Screen shot.

To invoke one of the methods, simply type in some parameter values for the method and click on the "Invoke" button. In this example, we're going to invoke the add method and pass in parameters 3 and 5:

Screen shot.

Once you invoke the add method, you will see a resulting page that will look like this:

Screen shot.

Note that the number 8 is the result of adding 3 and 5.

.NET Framework Essentials.NET Framework Essentials
By Thuan Thai & Hoang Lam
June 2001
0-596-00165-7, Order Number: 1657
320 pages, $29.95

Where Do I go Next?

You can use web services from VB and C# applications as well as ASP.NET web pages and any other programming language that conforms to the web service standards. In a future article, I will show you how to use the MathLibrary web service we developed from a Visual Basic.NET Windows application.

Hamid Shojaee is a Senior Consultant with Microsoft Consulting Services and co-founder of Vitrix Corporation -- a time and attendance software company specializing in web-based time tracking solutions.

Return to the .NET DevCenter.

Copyright © 2009 O'Reilly Media, Inc.