Build a Simple Web Serviceby 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:
Add(Integer1, Integer2)-- Adds two numbers and returns the result.
Multiply(Integer1, Integer2)-- Multiplies two numbers and returns the result.
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:
- Windows 2000 (any version)
- Internet Information Services (IIS)
- Visual Studio .NET Beta 1
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:
- From the File menu, select "New" and then "Project"
- 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.
- For the Name, type "MathLibrary" and for Location, keep the default, which should be "http://[your computer name]"
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,
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
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!
Pages: 1, 2