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.
Prerequisites
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,
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
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!
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
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:

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

Note
that the number 8 is the result of adding 3 and 5.
Ok.Friends...
I will tell you some Interesting Web services in another one article.
With Love,
K.Senthil Kumar
|